Google数据存储区 - 查询关键值

时间:2012-12-02 19:54:27

标签: java google-app-engine google-cloud-datastore

我有一个EntityKind SuggestedInterest。 当我使用键“GrpId”和属性“suggestedint”填充它时。

现在,我需要所请求的“GrpId”的“suggestedint”值

所以,我把查询写成:

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

当我执行此代码时,我得到了

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

开发人员文档被告知要使用Entity.KEY_RESERVED_PROPERTY查询密钥,但我想我误会了。查询密钥的正确方法是什么?

1 个答案:

答案 0 :(得分:12)

您应该传递Key而不是String

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

然后使用它:

 Filter filter = 
    new FilterPredicate(Entity.KEY_RESERVED_PROPERTY,FilterOperator.EQUAL,grpKey);