Is this the correct way to query by key with Java in Google Cloud Datastore? -
// utilize query.builder assemble query query.builder q = query.newbuilder(); q.addkindbuilder().setname("trivia"); q.setfilter(makefilter("__key__", propertyfilter.operator.equal, makevalue(makekey("trivia", "hgtg")))); // assemble runqueryrequest runqueryrequest request = runqueryrequest.newbuilder().setquery(q).build(); runqueryresponse response = main.datastore.runquery(request);
where key hgtg , kind trivia.
also, query considered consistent? far can tell querying 1 entity grouping should consistent.
thanks!
edit: i'm asking google cloud datastore has different java api app engine.
as per cloud datastore "getting started in java" page should rather utilize lookuprequest
:
// create rpc request begin new transaction. begintransactionrequest.builder treq = begintransactionrequest.newbuilder(); // execute rpc synchronously. begintransactionresponse tres = datastore.begintransaction(treq.build()); // transaction handle response. bytestring tx = tres.gettransaction(); // create rpc request entities key. lookuprequest.builder lreq = lookuprequest.newbuilder(); // set entity key 1 `path_element`: no parent. key.builder key = key.newbuilder().addpathelement( key.pathelement.newbuilder() .setkind("trivia") .setname("hgtg")); // add together 1 key lookup request. lreq.addkey(key); // set transaction, consistent snapshot of // entity @ time transaction started. lreq.getreadoptionsbuilder().settransaction(tx); // execute rpc , response. lookupresponse lresp = datastore.lookup(lreq.build()); // create rpc request commit transaction. commitrequest.builder creq = commitrequest.newbuilder(); // set transaction commit. creq.settransaction(tx); entity entity; if (lresp.getfoundcount() > 0) { entity = lresp.getfound(0).getentity(); }
as strong consistency, lookup consistent as written here :
remember, if lookup, ancestor query, or operation within transaction, see written data.
java google-cloud-datastore
No comments:
Post a Comment