问题
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