Google datastore - querying on key values

2019-04-27 02:05发布

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 ?

1条回答
爷、活的狠高调
2楼-- · 2019-04-27 02:23

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);
查看更多
登录 后发表回答