Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google datastore - querying on key values

I have a EntityKind SuggestedInterest. When I populate that with a key "GrpId" and property "suggestedint".

Now, I need the "suggestedint" value for a requested "GrpId"

So, I write the query as:

String findSuggestedInterest(String grpId)
{
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Filter filter = new FilterPredicate(Entity.KEY_RESERVED_PROPERTY,FilterOperator.EQUAL,grpId);
    Query q0 = new Query("SuggestedInterest").setFilter(filter);
    PreparedQuery pq0 = datastore.prepare(q0);
    Entity result = pq0.asSingleEntity();
    return result.getProperty("suggestedint").toString();       
}

When I execute this code I get

java.lang.IllegalArgumentException: __key__ filter value must be a Key

The developer docs told to use Entity.KEY_RESERVED_PROPERTY to query on keys, but I guess I misunderstood. What is the correct way to query on key ?

like image 203
anu Avatar asked Dec 02 '12 19:12

anu


People also ask

Is Google Datastore deprecated?

Because Cloud Datastore API v1 is released, Cloud Datastore API v1beta3 is now deprecated.

What is the query language we can use with Datastore?

The Python Datastore API provides two classes for preparing and executing queries: Query uses method calls to prepare the query. GqlQuery uses a SQL-like query language called GQL to prepare the query from a query string.

What are the two consistency levels for Datastore queries?

In Datastore, there are only two APIs that provide a strongly consistent view for reading entity values and indexes: (1) the lookup by key method and (2) the ancestor query.

What is a key in Datastore?

Each entity in a Datastore mode database has a key that uniquely identifies it. The key consists of the following components: The namespace of the entity, which allows for multitenancy. The kind of the entity, which categorizes it for the purpose of queries. An identifier for the individual entity, which can be either.


1 Answers

You should pass it a Key instead of String:

Key grpKey = KeyFactory.createKey("SuggestedInterest", grpId)

then use it:

 Filter filter = 
    new FilterPredicate(Entity.KEY_RESERVED_PROPERTY,FilterOperator.EQUAL,grpKey);
like image 135
Peter Knego Avatar answered Oct 24 '22 09:10

Peter Knego