I am using Google Cloud\'s Datastore Client Library for Java to access the Cloud Datastore.
Note: I am not using App Engine to deploy my applicatio
The line below always connects to the remote datastore
. Uses the default options (e.g. project, auth credentials) from gcloud
settings.
Datastore datastore = DatastoreOptions.defaultInstance().service();
To connect to the local datastore, try the below:
@Test
public void test1() throws IOException, InterruptedException {
Datastore ds = DatastoreOptions.builder().host("http://localhost:9999").projectId("my-project").build().service();
com.google.cloud.datastore.Key key = ds.newKeyFactory().kind("MyEntity").newKey("mykey");
com.google.cloud.datastore.Entity entity = com.google.cloud.datastore.Entity.builder(key).set("p1", "Hello World!").build();
entity = ds.put(entity);
entity = ds.get(key);
System.out.println(entity);
}
I started my Datastore Emulator on localhost:9999
. Set that as the host when building the DatastoreOptions.
I've confirmed that the Emulator console shows requests are received and entities are persisted. I've also checked the data file (local_db.bin
) and it shows the data (of course it is not a plain text file).
The one thing I don't know is - if there is a way to manage the local datastore using a browser interface. I could not find much documentation on how to administer the local datastore just like how we do the remote one from Cloud Console. Perhaps someone else can help with this.