使用 Java 的 Google 数据存储模拟器(不使用 GAE)

2023-11-23

我正在使用 Google Cloud 的 Java 数据存储客户端库来访问 Cloud Datastore。

Note:我没有使用 App Engine 来部署我的应用程序;只是出于开发目的运行本地应用程序。

按照示例,我可以读取/写入云数据存储。

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

我希望能够写入本地数据存储模拟器实例。 跟随指南here, I run gcloud beta emulators datastore start。 这显示在我的终端中:

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]

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

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

我运行我的应用程序并进行 REST 调用来发布或检索实体,但这仅针对云数据存储进行读/写。前往localhost:8964/datastore给我Not Found。虽然启动模拟器告诉我它创建了local_db.bin文件,据称包含该文件的文件夹是空的。 我还想避免使用 LocalDatastoreHelper 来访问本地模拟器。有什么方法可以仅使用 gcloud 来实现吗?


我假设您想要针对数据存储模拟器进行测试。在这种情况下,无需从 shell 启动数据存储模拟器。有一个本地数据存储助手在 gcloud 库中,您可以轻松创建、启动、重置和停止本地数据存储模拟器。

我没有找到任何相关文档,因此我为您创建了这个测试用例:

import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.KeyFactory;
import com.google.cloud.datastore.testing.LocalDatastoreHelper;
import org.junit.*;

import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

/**
 * This testcase demonstrate the use of the datastore emulator in JUnit test cases.
 *
 * from @link https://www.kontaktlinsen-preisvergleich.de
 */
public class DatastoreEmulatorTest {

    protected static LocalDatastoreHelper localDatastoreHelper;

    protected Datastore datastore;

    protected KeyFactory keyFactory;

    @BeforeClass
    public static void setUpClass() throws InterruptedException, IOException {
        // create and start a local datastore emulator on a random free port
        // this also means that you probably can run tests like this concurrently.
        System.out.println("[Datastore-Emulator] start");
        localDatastoreHelper = LocalDatastoreHelper.create();
        localDatastoreHelper.start();
        System.out.println("[Datastore-Emulator] listening on port: " + localDatastoreHelper.getPort());

        // set the system property to tell the gcloud lib to use the datastore emulator
        System.setProperty("DATASTORE_EMULATOR_HOST","localhost:" + localDatastoreHelper.getPort());
    }

    @Before
    public void setUp() {
        // create the datastore instance
        // because of the system property set it in setUpClass() this
        // datastore will be connected with the datastore emulator.
        datastore = DatastoreOptions.getDefaultInstance().getService();
        keyFactory = datastore.newKeyFactory().setKind("TestEntity");
    }

    @After
    public void tearDown() throws IOException {
        System.out.println("[Datastore-Emulator] reset");
        // this resets the datastore after every test
        localDatastoreHelper.reset();
    }

    @AfterClass
    public static void tearDownClass() throws InterruptedException, IOException {
        System.out.println("[Datastore-Emulator] stop");
        // this stops the datastore emulator after all tests are done
        localDatastoreHelper.stop();
    }

    @Test
    public void test1() {
        // stores an entity in the datastore and retrieves it later

        // create an Entity "TestEntity"
        Entity.Builder builder = Entity.newBuilder(keyFactory.newKey(42));
        builder.set("name", "Test1");

        // store it in datastore
        datastore.put(builder.build());

        // retrieve entity by key
        Entity entity = datastore.get(keyFactory.newKey(42));
        assertNotNull(entity);
        assertEquals("Test1", entity.getString("name"));
    }

    @Test
    public void test2() {
        // try to access the entity created in test1, shouldn't work because
        // of calling reset in tearDown() after each test.

        // try to retrieve entity by key
        Entity entity = datastore.get(keyFactory.newKey(42));
        assertNull(entity);
    }
}

LocalDatastoreHelper 在空闲端口上创建一个数据存储模拟器实例,并且不存储到磁盘 - 当您使用调试器停止测试用例并查找进程时,您会发现如下内容:

$ ps ax | grep CloudDatastore
2614   ??  R      0:01.39 /usr/bin/java -cp /Users/marco/google-cloud-sdk/platform/cloud-datastore-emulator/CloudDatastore.jar com.google.cloud.datastore.emulator.CloudDatastore /Users/marco/google-cloud-sdk/platform/cloud-datastore-emulator/cloud_datastore_emulator start --host=localhost --port=57640 --store_on_disk=False --consistency=0.9 --allow_remote_shutdown /var/folders/ky/c126qk_161159ltyrbpdxv8w0000gn/T/gcd2141205756617995044

这也意味着您也应该能够并行运行测试。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Java 的 Google 数据存储模拟器(不使用 GAE) 的相关文章

随机推荐

  • Spring自定义注解多字段验证

    这里有一个有点贪心的问题 希望这个问题也能帮助其他想了解更多注释验证的人 我目前正在学习Spring 目前 我计划尝试一下自定义注释验证 我查了很多 现在知道主要有两种验证 一种是用于控制器的 另一种是使用 Valid的注解方法 这是我的场
  • PHP 在所有请求中保留变量

    在某些语言 C 或 NET 中 这将是静态变量 但在 PHP 中 每次请求后都会清除内存 我希望该值在所有请求中持续存在 我不想 SESSION 因为每个用户的情况都不同 为了帮助解释这里有一个例子 我想要一个像这样的可以计数的脚本 无论哪
  • 在 File.Create 之后关闭文件[重复]

    这个问题在这里已经有答案了 我检查文件是否存在 if File Exists myPath File Create myPath 但是 当我去创建一个StreamReader使用这个新创建的文件 我收到一条错误消息 该进程无法访问文件 此处
  • 摆脱停用词和标点符号

    我正在与 NLTK 停用词作斗争 这是我的代码 有人可以告诉我出了什么问题吗 from nltk corpus import stopwords def removeStopwords palabras return word for wo
  • 为什么在 HTC Desire 上发送短信时出现 NullPointerException,或者什么是 SubmitPdu?

    所以我得到这个堆栈跟踪 java lang NullPointerException at android telephony SmsMessage SubmitPdu
  • Akka:如何查找集群中的当前节点?

    从 Akka actor 内部 如何找到集群的节点 即本节点认为当前可以访问的节点 谢谢 丹尼尔 您实际上不需要订阅ClusterDomainEvent or MemberEvent 您只需访问state集群扩展的成员 例如 val clu
  • 相同美学的多个传说

    我正在尝试使用facet grid or facet wrap和这个结合geom raster 然而 在每个面板中 z审美是不同尺度的 例如 Data at end of question ggplot dd aes x y geom ra
  • 没有可以捕获托管变量的 lambda 的解决方法

    在 C CLI 中 您无法创建托管 lambda 就像在 C 中一样 因此无法捕获托管变量 您可以创建常规方法 而不是 lambda 但您仍然无法捕获托管变量 C CLI 代码中是否有可采用的标准解决方法 换句话说 我正在寻找一个可以在 C
  • 如何在 symfony2 中禁用数据库配置

    我正在开发的新应用程序正在使用 Symfony2 没有可供使用的数据库连接 相反 它是建立在许多 Web 服务调用之上的 在 Symfony app config 下 我想删除所有 database 条目 但是当我这样做时 我得到一个 Pa
  • 如何在 elastic beanstalk 中扩展 nginx 配置 (Amazon Linux 2)

    我听从了建议here配置 nginx 反向代理以允许大于默认 1mb 的文件 所以 我的代码在 platform nginx conf d prod conf看起来像这样 http client max body size 30M 然而 这
  • 适用于 iPhone 的 UIPOPOVER?或者是假的?

    谁能看一下这个吗 http www woowoomac com storage awesome note iphone note todo app menus jpg SQUARESPACE CACHEVERSION 12685817624
  • 如何让 CRON 调用正确的路径

    我试图让 cron 调用正确的路径 当我从 shell 运行 Python 脚本时 脚本运行良好 因为它使用 bashrc 中设置的 PATH 但是当我使用 cron 时 所有 PATH 都不会从 bashrc 使用 是否有一个文件可以像
  • WCF 4 服务的扁平 WSDL

    使用 WCF 3 5 和来自 Christian Weyer 的 FlatWsdl EndpointBehavior 我能够为我的 WCF 服务获取单个平面 WSDL 文件 而无需任何
  • DDD-- 如何补充水分

    Question 从存储库中重新水化聚合体的最佳 高效且面向未来的方法是什么 所提供的方法有哪些优点和缺点 我的看法是否正确 假设我们有一个带有私有 setter 和公共 getter 的聚合根用于访问state 行为是通过聚合根上的方法完
  • Dynamic_cast 中的类型必须是完整类类型的指针或引用,或者 void *

    我希望有人能理解为什么下面的代码失败 我试图从 osg Node 节点对象获取 PositionAttitudeTransform Openscenegraph 类 的实例 但下面有粗体的编译器错误 void CameraPosCallba
  • 为什么扩展元素不适合复制多维数组?

    来自MDN 传播语法 注意 通常 ES2015 中的扩展运算符在复制数组时会深入一层 因此 它们不适合复制多维数组 这与 Object assign 和对象扩展语法的情况相同 请参阅下面的示例以更好地理解 var a 1 2 3 var b
  • Codeigniter 如何创建 PDF

    我要创建一个发票系统 我现在正在为此做准备 我将使用 Codeigniter 问题是我想创建 PDF 格式的发票 这样我就可以通过电子邮件发送它 你们有什么建议 我正在考虑 HTML 到 PDF 的转换 或者在屏幕上显示发票并安装 pdf
  • Boost 随机和 OpenMP

    我从 OpenMP 并行代码部分收到 总线错误 我在下面重新创建了我的问题的简单版本 该代码本质上对函数进行了多次调用uniform distribution 它使用 Boost 的uniform int distribution 绘制 0
  • SQL Server 2005 - 将列设置为只读

    我在 SQL Server 2005 数据库的表中有一个 InsertTime 字段 当记录首次插入数据库时 该字段默认为 getDate 我想确保此专栏不再更新 是否可以将此列设置为只读 或者是否有更好的方法来执行此操作 而无需为开发人员
  • 使用 Java 的 Google 数据存储模拟器(不使用 GAE)

    我正在使用 Google Cloud 的 Java 数据存储客户端库来访问 Cloud Datastore Note 我没有使用 App Engine 来部署我的应用程序 只是出于开发目的运行本地应用程序 按照示例 我可以读取 写入云数据存