使用Java的Google数据存储模拟器(不使用GAE) [英] Google Datastore Emulator using Java (Not using GAE)

查看:131
本文介绍了使用Java的Google数据存储模拟器(不使用GAE)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google Cloud的Datastore Client Library for Java来访问云数据存储。



注意:我没有使用App Engine部署我的应用程序;只需运行本地应用程序进行开发即可。



以下示例可以读取/写入Cloud Datastore。

  Datastore datastore = DatastoreOptions.defaultInstance()。service(); 
KeyFactory keyFactory = datastore.newKeyFactory()。setKind(MyKind);
Key key = keyFactory.newKey();
实体实体= datastore.get(key);

我希望能够写入本地数据存储模拟器实例。
按照这里的指导,我运行 gcloud beta模拟器数据存储开始
这显示在我的终端:

  C:\Users\User> gcloud beta模拟器数据存储开始
警告:重新使用[C:\ Users \ User\AppData\Roaming\gcloud\emulators\datastore〕中的现有数据。
执行:cmd / c C:\ Users \User\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\cloud-datastore-emulator\cloud_datastore_emulator .cmd start --host = localhost --port = 8964 --store_on_disk = True --consistency = 0.9 --allow_remote_shutdown C:\Users\User\AppData\Roaming\gcloud\emulators\datastore
[datastore] Oct 31,2016 11:37:27 AM com.google.cloud.datastore.emulator.CloudDatastore $ FakeDatastoreAction $ 7 apply
[datastore]信息:提供--allow_remote_shutdown启动命令更长的必要。
[datastore] Oct 31,2016 11:37:27 AM com.google.cloud.datastore.emulator.impl.LocalDatastoreFileStub< init>
[datastore] INFO:本地数据存储已初始化:
[数据存储]类型:高复制
[数据存储]存储:C:\ Users \ Users \AppData\Roaming\gcloud \ emulators\datastore\WEB-INF\appengine-generated\local_db.bin
[datastore] Oct 31,2016 11:37:28 AM io.grpc.internal.ManagedChannelImpl< init>
[datastore] INFO:[ManagedChannelImpl @ 5e955596]使用target localhost创建:8964
[datastore] 2016年10月31日上午11:37:28 com.google.cloud.datastore.emulator.impl.LocalDatastoreFileStub加载
[数据存储] INFO:后备存储,C:\ Users \User\AppData\Roaming\gcloud\emulators\datastore\WEB-INF\appengine-generated\local_db .bin,不存在。它将被创建。
[datastore] Oct 31,2016 11:37:28 AM io.gapi.emulators.netty.NettyUtil applyJava7LongHostnameWorkaround
[datastore]信息:无法应用Java 7 long hostname解决方法。
[datastore] API端点:http:// localhost:8964
[datastore]如果您使用支持DATASTORE_EMULATOR_HOST环境变量的库,请运行:
[datastore]
[数据存储]导出DATASTORE_EMULATOR_HOST =本地主机:8964
[数据存储]
[数据存储] Dev App Server现在正在运行。
[datastore]

我打开另一个终端并设置环境变量:

  C:\Users\User> gcloud beta模拟器数据存储env-init> set_vars.cmd&& set_vars.cmd 
C:\Users\User> set DATASTORE_DATASET = my-project-id
C:\Users\User> set DATASTORE_EMULATOR_HOST = localhost:8964
C:\\ \\ Users \\ Users \\ gt; set DATASTORE_EMULATOR_HOST_PATH = localhost:8964 / datastore
C:\Users\User> set DATASTORE_HOST = http:// localhost:8964
C:\ Users \ User>设置DATASTORE_PROJECT_ID = my-project-id

我运行我的应用程序并进行REST调用以发布或检索一个实体,但这只能读取/写入云数据存储。标题为 localhost:8964 / datastore 给我未找到。尽管启动模拟器告诉我它已经创建了 local_db.bin 文件,据说包含它的文件夹是空的。
我也想远离使用LocalDatastoreHelper来访问本地模拟器。是否有任何方法可以实现使用gcloud?

解决方案

下面的代码总是连接到远程数据存储。使用gcloud设置的默认选项(例如项目,授权凭证)。

  Datastore datastore = DatastoreOptions.defaultInstance()。service(); 

要连接到本地数据存储,请尝试以下操作:


@Test
public void test1()throws IOException,InterruptedException {
Datastore ds = DatastoreOptions.builder()。host(http:/ /本地主机:9999 )专案编号( 我的项目)建设()服务();
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);
}



我在localhost:9999上启动了我的数据存储模拟器。在构建DatastoreOptions时将其设置为主机。

我已确认模拟器控制台显示接收到请求并且实体持续存在。我也检查过数据文件(local_db.bin),它显示数据(当然它不是纯文本文件)。

我不知道的一件事是 - 如果有办法通过浏览器界面来管理本地数据存储。我无法找到有关如何管理本地数据存储的文档,就像我们如何从云端控制台执行远程数据存储一样。也许别人可以帮助解决这个问题。

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 application; just running a local application for development purposes.

Following the example, I can read/write to the Cloud Datastore.

Datastore datastore = DatastoreOptions.defaultInstance().service();
KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
Key key = keyFactory.newKey();
Entity entity = datastore.get(key);

I want to be able to write to a local Datastore emulator instance instead. Following the guide here, I run gcloud beta emulators datastore start. This shows up in my terminal:

C:\Users\User>gcloud beta emulators datastore start
WARNING: Reusing existing data in [C:\Users\User\AppData\Roaming\gcloud\emulators\datastore].
Executing: cmd /c C:\Users\User\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\cloud-datastore-emulator\cloud_datastore_emulator.cmd start --host=localhost --port=8964 --store_on_disk=True --consistency=0.9 --allow_remote_shutdown C:\Users\User\AppData\Roaming\gcloud\emulators\datastore
[datastore] Oct 31, 2016 11:37:27 AM com.google.cloud.datastore.emulator.CloudDatastore$FakeDatastoreAction$7 apply
[datastore] INFO: Provided --allow_remote_shutdown to start command which is no longer necessary.
[datastore] Oct 31, 2016 11:37:27 AM com.google.cloud.datastore.emulator.impl.LocalDatastoreFileStub <init>
[datastore] INFO: Local Datastore initialized:
[datastore]     Type: High Replication
[datastore]     Storage: C:\Users\User\AppData\Roaming\gcloud\emulators\datastore\WEB-INF\appengine-generated\local_db.bin
[datastore] Oct 31, 2016 11:37:28 AM io.grpc.internal.ManagedChannelImpl <init>
[datastore] INFO: [ManagedChannelImpl@5e955596] Created with target localhost:8964
[datastore] Oct 31, 2016 11:37:28 AM com.google.cloud.datastore.emulator.impl.LocalDatastoreFileStub load
[datastore] INFO: The backing store, C:\Users\User\AppData\Roaming\gcloud\emulators\datastore\WEB-INF\appengine-generated\local_db.bin, does not exist. It will be created.
[datastore] Oct 31, 2016 11:37:28 AM io.gapi.emulators.netty.NettyUtil applyJava7LongHostnameWorkaround
[datastore] INFO: Unable to apply Java 7 long hostname workaround.
[datastore] API endpoint: http://localhost:8964
[datastore] If you are using a library that supports the DATASTORE_EMULATOR_HOST environment variable, run:
[datastore]
[datastore]   export DATASTORE_EMULATOR_HOST=localhost:8964
[datastore]
[datastore] Dev App Server is now running.
[datastore]

I open another terminal and set the environment variables:

C:\Users\User>gcloud beta emulators datastore env-init > set_vars.cmd && set_vars.cmd
C:\Users\User>set DATASTORE_DATASET=my-project-id
C:\Users\User>set DATASTORE_EMULATOR_HOST=localhost:8964
C:\Users\User>set DATASTORE_EMULATOR_HOST_PATH=localhost:8964/datastore
C:\Users\User>set DATASTORE_HOST=http://localhost:8964
C:\Users\User>set DATASTORE_PROJECT_ID=my-project-id

I run my application and make a REST call to post or retrieve an Entity, but this only reads/writes against the Cloud Datastore. Heading to localhost:8964/datastore gives me Not Found. Although starting the emulator tells me that it created local_db.bin file, the folder supposedly containing it is empty. I also want to stay away from using LocalDatastoreHelper in order to access the local emulator. Is there any way to achieve using gcloud only ?

解决方案

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 on this.

这篇关于使用Java的Google数据存储模拟器(不使用GAE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆