Google Datastore Emulator using Java (Not using GAE)

后端 未结 2 533
死守一世寂寞
死守一世寂寞 2021-01-18 06:35

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

2条回答
  •  星月不相逢
    2021-01-18 07:08

    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.

提交回复
热议问题