Class Query<V> (2.23.0)

public abstract class Query<V> implements Serializable

A Google Cloud Datastore query. For usage examples see GqlQuery, StructuredQuery and AggregationQuery.

Note that queries require proper indexing. See Cloud Datastore Index Configuration for help configuring indexes. See Also: Datastore Queries

Inheritance

java.lang.Object > Query<V>

Implements

Serializable

Type Parameter

Name Description
V

Static Methods

<V>newGqlQueryBuilder(Query.ResultType<V> resultType, String gql)

public static GqlQuery.Builder<V> <V>newGqlQueryBuilder(Query.ResultType<V> resultType, String gql)

Returns a new GqlQuery builder.

Example of creating and running a typed GQL query.


 String kind = "my_kind";
 String gqlQuery = "select * from " + kind;
 Query<Entity> query = Query.newGqlQueryBuilder(Query.ResultType.ENTITY, gqlQuery).build();
 QueryResults<Entity> results = datastore.run(query);
 // Use results
 

See Also: GQL Reference

Parameters
Name Description
resultType ResultType<V>
gql String
Returns
Type Description
Builder<V>

newAggregationQueryBuilder()

public static AggregationQuery.Builder newAggregationQueryBuilder()

Returns a new AggregationQuery builder.

Example of creating and running an AggregationQuery.

StructuredQuery example:


 EntityQuery selectAllQuery = Query.newEntityQueryBuilder()
    .setKind("Task")
    .build();
 AggregationQuery aggregationQuery = Query.newAggregationQueryBuilder()
    .addAggregation(count().as("total_count"))
    .over(selectAllQuery)
    .build();
 AggregationResults aggregationResults = datastore.runAggregation(aggregationQuery);
 // Use aggregationResults
 

GqlQuery example:


 GqlQuery selectAllGqlQuery = Query.newGqlQueryBuilder(
         "AGGREGATE COUNT(*) AS total_count OVER(SELECT * FROM Task)"
     )
     .setAllowLiteral(true)
     .build();
 AggregationQuery aggregationQuery = Query.newAggregationQueryBuilder()
     .over(selectAllGqlQuery)
     .build();
 AggregationResults aggregationResults = datastore.runAggregation(aggregationQuery);
 // Use aggregationResults
 
Returns
Type Description
AggregationQuery.Builder

newEntityQueryBuilder()

public static EntityQuery.Builder newEntityQueryBuilder()

Returns a new StructuredQuery builder for full (complete entities) queries.

Example of creating and running an entity query.


 String kind = "my_kind";
 Query<Entity> query = Query.newEntityQueryBuilder().setKind(kind).build();
 QueryResults<Entity> results = datastore.run(query);
 // Use results
 
Returns
Type Description
EntityQuery.Builder

newGqlQueryBuilder(String gql)

public static GqlQuery.Builder<?> newGqlQueryBuilder(String gql)

Returns a new GqlQuery builder.

Example of creating and running a GQL query.


 String kind = "my_kind";
 String gqlQuery = "select * from " + kind;
 Query query = Query.newGqlQueryBuilder(gqlQuery).build();
 QueryResults results = datastore.run(query);
 // Use results
 

See Also: GQL Reference

Parameter
Name Description
gql String
Returns
Type Description
Builder<?>

newKeyQueryBuilder()

public static KeyQuery.Builder newKeyQueryBuilder()

Returns a new StructuredQuery builder for key only queries.

Example of creating and running a key query.


 String kind = "my_kind";
 Query<Key> query = Query.newKeyQueryBuilder().setKind(kind).build();
 QueryResults<Key> results = datastore.run(query);
 // Use results
 
Returns
Type Description
KeyQuery.Builder

newProjectionEntityQueryBuilder()

public static ProjectionEntityQuery.Builder newProjectionEntityQueryBuilder()

Returns a new StructuredQuery builder for projection queries.

Example of creating and running a projection entity query.


 String kind = "my_kind";
 String property = "my_property";
 Query<ProjectionEntity> query = Query.newProjectionEntityQueryBuilder()
     .setKind(kind)
     .addProjection(property)
     .build();
 QueryResults<ProjectionEntity> results = datastore.run(query);
 // Use results
 
Returns
Type Description
ProjectionEntityQuery.Builder

Methods

getNamespace()

public String getNamespace()
Returns
Type Description
String