如何使用java在Google云数据存储区中同时放置/获取多个实体

时间:2014-10-08 14:11:15

标签: java google-cloud-datastore

documentation中,我没有找到如何一次放置或检索多个实体。此外,通过使用GQL,我无法执行查询,例如select * from k where __ key __ in(' key1',' key2',' key3' )。

任何人都可以帮助我,我如何使用java一次插入/检索多个实体?

谢谢,

3 个答案:

答案 0 :(得分:2)

在Cloud Datastore中,LookupRequestCommitRequest分别允许指定多个密钥和实体。例如:

LookupRequest request = LookupRequest.newBuilder()
    .addKey(key1)
    .addKey(key2)
    .build();

或:

CommitRequest request = CommitRequest.newBuilder()
    .setMode(CommitRequest.Mode.NON_TRANSACTIONAL)
    .setMutation(Mutation.newBuilder()
        .addInsert(entity1)
        .addInsert(entity2))
    .build();  

这些是这些页面上给出的示例的略微变化: https://cloud.google.com/datastore/docs/concepts/entities#Datastore_Retrieving_an_entity https://cloud.google.com/datastore/docs/concepts/entities#Datastore_Creating_an_entity

Cloud Datastore GQL目前不支持IN或写入。

答案 1 :(得分:0)

虽然您的问题使用GQL,但它标记为java,所以我会给出两个答案,价格为一个。

您可以使用get(Iterable<Key>)中的put(Iterable<Entity>)DatastoreService一次获取并放置多个实体。 javadocs提供了有用的参考。

如果你想在datastore viewer中使用GQL,你需要这样做

SELECT * FROM Kind where __key__ in (key('Kind', id1), key('Kind', id2), ...)

答案 2 :(得分:0)

要通过指定其键来检索多个实体,您应该使用GetMulti而不是编写查询。 https://cloud.google.com/appengine/docs/go/datastore/reference#GetMulti