Class AggregationQuery (2.31.4)

publicclass AggregationQueryextendsQuery<AggregationResults>

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.

StructuredQuery example:


EntityQueryselectAllQuery=Query.newEntityQueryBuilder()
.setKind("Task")
.build();
AggregationQueryaggregationQuery=Query.newAggregationQueryBuilder()
.addAggregation(count().as("total_count"))
.over(selectAllQuery)
.build();
AggregationResultsaggregationResults=datastore.runAggregation(aggregationQuery);
for(AggregationResultaggregationResult:aggregationResults){
System.out.println(aggregationResult.get("total_count"));
}

GqlQuery example:


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"));
}

See Also: Datastore queries

Inheritance

java.lang.Object > Query > AggregationQuery

Methods

getAggregations()

publicSet<Aggregation>getAggregations()

Returns the Aggregation(s) for this Query.

Returns
Type Description
Set<Aggregation>

getMode()

publicAggregationQuery.ModegetMode()

Returns the Mode for this query.

Returns
Type Description
AggregationQuery.Mode

getNestedGqlQuery()

publicGqlQuery<?>getNestedGqlQuery()

Returns the underlying for this Query. Returns null if created with StructuredQuery

Returns
Type Description
GqlQuery<?>

getNestedStructuredQuery()

publicStructuredQuery<?>getNestedStructuredQuery()

Returns the underlying for this Query. Returns null if created with GqlQuery

Returns
Type Description
StructuredQuery<?>

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年11月19日 UTC.