How to put/get multiple entities at once in Google cloud Datastore using java

℡╲_俬逩灬. 提交于 2019-12-11 11:19:10

问题


In the documentation, I didn't find how I can put or retrieve multiple entities at once. further, by using GQL, I was not able to execute queries such select * from k where __ key __ in ('key1','key2','key3').

Can any one help me please, how I can insert/ retrieve multiple entities at once using java??

Thanks,


回答1:


In Cloud Datastore, a LookupRequest and CommitRequest allow multiple keys and entities, respectively, to be specified. For example:

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

or:

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

These are slight variations on the examples given on these pages: 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 does not currently support IN or writes.




回答2:


Although your question uses GQL, it is tagged java, so I'll give you two answers for the price of one.

You can use get(Iterable<Key>) and put(Iterable<Entity>) in the DatastoreService to get and put multiple entities at once. The javadocs provide a useful reference.

If you want to use GQL in the datastore viewer, you'll need to do so like this

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



回答3:


To retrieve multiple entities by specifying their keys, you should use GetMulti instead of writing a query. https://cloud.google.com/appengine/docs/go/datastore/reference#GetMulti



来源:https://stackoverflow.com/questions/26259068/how-to-put-get-multiple-entities-at-once-in-google-cloud-datastore-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!