How to specify server to access datastore from java app?

2 views
Skip to first unread message

zeke14 via StackOverflow

unread,
Mar 8, 2016, 3:00:06 PM3/8/16
to google-appengin...@googlegroups.com

I'm trying to access Google Datastore from a standalone java app (not a web application or servlet), using the low-level java api, running on my local system.

remoteapi appears the simplest way do this. However, I need to specify a server per

RemoteApiOptions options = new RemoteApiOptions() .server("your_app_id.appspot.com", 443) .useApplicationDefaultCredential();

How do I specify the server(...) invocation to access Datastore? I have run

gcloud auth login

and I have a project configured for Datastore (with a project ID, etc.) but no 'app_id' since there's no app involved.

Is there a standard server for accessing Datastore? Or is there a better way to access Datastore from a standalone java app?

thanks



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/35876900/how-to-specify-server-to-access-datastore-from-java-app

zeke14 via StackOverflow

unread,
Mar 8, 2016, 3:10:08 PM3/8/16
to google-appengin...@googlegroups.com

Alex Martelli via StackOverflow

unread,
Mar 8, 2016, 7:20:04 PM3/8/16
to google-appengin...@googlegroups.com

To access Cloud Datastore outside of App Engine, follow the docs at https://cloud.google.com/datastore/docs/datastore-api-tutorial , which do not involve the Remote API. The complete example given on that page uses Maven (for the Java version), but, if that's not how you prefer to develop, you can look at the sources just as a usage example, and separately install the Google Cloud Client Library for Java; for running standalone (not in App Engine or Compute Engine), you'll explicitly authorize, as per the example on that page...:

import com.google.gcloud.AuthCredentials;
import com.google.gcloud.datastore.Datastore;
import com.google.gcloud.datastore.DatastoreOptions;
import com.google.gcloud.datastore.Entity;
import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.KeyFactory;

DatastoreOptions options = DatastoreOptions.builder()
  .projectId(PROJECT_ID)
  .authCredentials(AuthCredentials.createForJson(
    new FileInputStream(PATH_TO_JSON_KEY))).build();
Datastore datastore = options.service();
KeyFactory keyFactory = datastore.newKeyFactory().kind(KIND);
Key key = keyFactory.newKey(keyName);
Entity entity = datastore.get(key);


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/35876900/how-to-specify-server-to-access-datastore-from-java-app/35880617#35880617
Reply all
Reply to author
Forward
0 new messages