An implementation of a Google Cloud Datastore Query that returns AggregationResults, It
can be constructed by providing a nested query (StructuredQuery or GqlQuery) to
run the aggregations on and a set of Aggregation.
GqlQueryselectAllGqlQuery=Query.newGqlQueryBuilder("AGGREGATE COUNT(*) AS total_count, COUNT_UP_TO(100) AS count_upto_100 OVER(SELECT * FROM Task)").setAllowLiteral(true).build();AggregationQueryaggregationQuery=Query.newAggregationQueryBuilder().over(selectAllGqlQuery).build();AggregationResultsaggregationResults=datastore.runAggregation(aggregationQuery);for(AggregationResultaggregationResult:aggregationResults){System.out.println(aggregationResult.get("total_count"));System.out.println(aggregationResult.get("count_upto_100"));}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Class AggregationQuery (2.31.2)\n\nVersion latestkeyboard_arrow_down\n\n- [2.31.2 (latest)](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.AggregationQuery)\n- [2.31.1](/java/docs/reference/google-cloud-datastore/2.31.1/com.google.cloud.datastore.AggregationQuery)\n- [2.30.0](/java/docs/reference/google-cloud-datastore/2.30.0/com.google.cloud.datastore.AggregationQuery)\n- [2.29.1](/java/docs/reference/google-cloud-datastore/2.29.1/com.google.cloud.datastore.AggregationQuery)\n- [2.28.2](/java/docs/reference/google-cloud-datastore/2.28.2/com.google.cloud.datastore.AggregationQuery)\n- [2.27.1](/java/docs/reference/google-cloud-datastore/2.27.1/com.google.cloud.datastore.AggregationQuery)\n- [2.26.4](/java/docs/reference/google-cloud-datastore/2.26.4/com.google.cloud.datastore.AggregationQuery)\n- [2.25.2](/java/docs/reference/google-cloud-datastore/2.25.2/com.google.cloud.datastore.AggregationQuery)\n- [2.24.3](/java/docs/reference/google-cloud-datastore/2.24.3/com.google.cloud.datastore.AggregationQuery)\n- [2.23.0](/java/docs/reference/google-cloud-datastore/2.23.0/com.google.cloud.datastore.AggregationQuery)\n- [2.22.0](/java/docs/reference/google-cloud-datastore/2.22.0/com.google.cloud.datastore.AggregationQuery)\n- [2.21.3](/java/docs/reference/google-cloud-datastore/2.21.3/com.google.cloud.datastore.AggregationQuery)\n- [2.20.2](/java/docs/reference/google-cloud-datastore/2.20.2/com.google.cloud.datastore.AggregationQuery)\n- [2.19.2](/java/docs/reference/google-cloud-datastore/2.19.2/com.google.cloud.datastore.AggregationQuery)\n- [2.18.5](/java/docs/reference/google-cloud-datastore/2.18.5/com.google.cloud.datastore.AggregationQuery)\n- [2.17.6](/java/docs/reference/google-cloud-datastore/2.17.6/com.google.cloud.datastore.AggregationQuery) \n\n public class AggregationQuery extends Query\u003cAggregationResults\u003e\n\nAn implementation of a Google Cloud Datastore Query that returns [AggregationResults](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.AggregationResults), It\ncan be constructed by providing a nested query ([StructuredQuery](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.StructuredQuery) or [GqlQuery](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.GqlQuery)) to\nrun the aggregations on and a set of [Aggregation](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.aggregation.Aggregation).\n\n[StructuredQuery](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.StructuredQuery) example: \n\n\n EntityQuery selectAllQuery = Query.newEntityQueryBuilder()\n .setKind(\"Task\")\n .build();\n AggregationQuery aggregationQuery = Query.newAggregationQueryBuilder()\n .addAggregation(count().as(\"total_count\"))\n .over(selectAllQuery)\n .build();\n AggregationResults aggregationResults = datastore.runAggregation(aggregationQuery);\n for (AggregationResult aggregationResult : aggregationResults) {\n System.out.println(aggregationResult.get(\"total_count\"));\n }\n \n[GqlQuery](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.GqlQuery) example: \n\n\n GqlQuery selectAllGqlQuery = Query.newGqlQueryBuilder(\n \"AGGREGATE COUNT(*) AS total_count, COUNT_UP_TO(100) AS count_upto_100 OVER(SELECT * FROM Task)\"\n )\n .setAllowLiteral(true)\n .build();\n AggregationQuery aggregationQuery = Query.newAggregationQueryBuilder()\n .over(selectAllGqlQuery)\n .build();\n AggregationResults aggregationResults = datastore.runAggregation(aggregationQuery);\n for (AggregationResult aggregationResult : aggregationResults) {\n System.out.println(aggregationResult.get(\"total_count\"));\n System.out.println(aggregationResult.get(\"count_upto_100\"));\n }\n \nSee Also: [Datastore queries](https://cloud.google.com/appengine/docs/java/datastore/queries) \n\nInheritance\n-----------\n\n[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \\\u003e [Query](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.Query) \\\u003e AggregationQuery \n\nInherited Members\n-----------------\n\n[Query.\\\u003cV\\\u003enewGqlQueryBuilder(Query.ResultType\\\u003cV\\\u003e,String)](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.Query#com_google_cloud_datastore_Query__V_newGqlQueryBuilder_com_google_cloud_datastore_Query_ResultType_V__java_lang_String_) \n[Query.getNamespace()](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.Query#com_google_cloud_datastore_Query_getNamespace__) \n[Query.newAggregationQueryBuilder()](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.Query#com_google_cloud_datastore_Query_newAggregationQueryBuilder__) \n[Query.newEntityQueryBuilder()](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.Query#com_google_cloud_datastore_Query_newEntityQueryBuilder__) \n[Query.newGqlQueryBuilder(String)](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.Query#com_google_cloud_datastore_Query_newGqlQueryBuilder_java_lang_String_) \n[Query.newKeyQueryBuilder()](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.Query#com_google_cloud_datastore_Query_newKeyQueryBuilder__) \n[Query.newProjectionEntityQueryBuilder()](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.Query#com_google_cloud_datastore_Query_newProjectionEntityQueryBuilder__) \n[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) \n[Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) \n[Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) \n[Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) \n[Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) \n[Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) \n[Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) \n[Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) \n[Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) \n[Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) \n[Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)\n\nMethods\n-------\n\n### getAggregations()\n\n public Set\u003cAggregation\u003e getAggregations()\n\nReturns the [Aggregation](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.aggregation.Aggregation)(s) for this Query.\n\n### getMode()\n\n public AggregationQuery.Mode getMode()\n\nReturns the Mode for this query.\n\n### getNestedGqlQuery()\n\n public GqlQuery\u003c?\u003e getNestedGqlQuery()\n\nReturns the underlying [for this Query](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.GqlQuery). Returns null if created with [StructuredQuery](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.StructuredQuery)\n\n### getNestedStructuredQuery()\n\n public StructuredQuery\u003c?\u003e getNestedStructuredQuery()\n\nReturns the underlying [for this Query](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.StructuredQuery). Returns null if created with\n[GqlQuery](/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.GqlQuery)"]]