Dataflow + Datastore = DatastoreException:I / O错误

时间:2016-09-05 16:55:02

标签: java google-cloud-platform google-cloud-datastore google-cloud-dataflow

我正在尝试使用spritesheet从DataFlow写入DataStore。

我的代码看起来像这样(受[1]中的例子的启发):

com.google.cloud.datastore

我收到了这个错误:

public void processElement(ProcessContext c) {
    LocalDatastoreHelper HELPER = LocalDatastoreHelper.create(1.0);
    Datastore datastore = HELPER.options().toBuilder().namespace("ghijklmnop").build().service();
    Key taskKey = datastore.newKeyFactory()
        .ancestors(PathElement.of("TaskList", "default"))
        .kind("Task")
        .newKey("sampleTask");
    Entity task = Entity.builder(taskKey)
        .set("category", "Personal")
        .set("done", false)
        .set("priority", 4)
        .set("description", "Learn Cloud Datastore")
        .build();
    datastore.put(task);
}

我尝试使用exception: "java.lang.RuntimeException: com.google.cloud.dataflow.sdk.util.UserCodeException: com.google.cloud.datastore.DatastoreException: I/O error at com.google.cloud.dataflow.sdk.runners.worker.SimpleParDoFn$1.output(SimpleParDoFn.java:162) at com.google.cloud.dataflow.sdk.util.DoFnRunnerBase$DoFnContext.sideOutputWindowedValue(DoFnRunnerBase.java:314) at com.google.cloud.dataflow.sdk.util.DoFnRunnerBase$DoFnProcessContext.sideOutput(DoFnRunnerBase.java:470) at com.google.cloud.dataflow.sdk.transforms.Partition$PartitionDoFn.processElement(Partition.java:172) 接收器,但看起来它在流媒体运行器中目前不受支持。

如何避免该错误?或者从DataFlow写入DataStore的最佳方法是什么?

[1] https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/datastore/src/main/java/com/google/datastore/snippets/Concepts.java

1 个答案:

答案 0 :(得分:2)

按照@Sam McVeety的建议,我试图在Dataflow之外隔离我的数据存储区代码。我确实得到了同样的错误!

但这也让我看到了异常的原因,我在Dataflow日志中没有看到:

Caused by: java.net.ConnectException: Connection refused

线索出现在我正在使用的导入行中:com.google.cloud.datastore.testing.LocalDatastoreHelper

它是测试的辅助工具,负责在本地模拟Datastore API。糟糕。

所以这是我在进行一些本地调试后得到的代码:

public void processElement(ProcessContext c) {
    final Datastore datastore = DatastoreOptions.defaultInstance().service();
    final KeyFactory keyFactory = datastore.newKeyFactory().kind("Task");

    Key key = datastore.allocateId(keyFactory.newKey());
    Entity task = Entity.builder(key)
        .set("description", StringValue.builder(":D").excludeFromIndexes(true).build())
        .set("created", DateTime.now())
        .set("done", false)
        .build();
    datastore.put(task);
}

主要区别在于:

LocalDatastoreHelper.create(1.0).options().toBuilder().namespace("ghijklmnop").build().service()

变为

DatastoreOptions.defaultInstance().service();