【问题标题】:Saving to Google Datastore from Google Dataflow从 Google Dataflow 保存到 Google Datastore
【发布时间】:2017-04-07 13:41:12
【问题描述】:

我正在尝试从 Google Dataflow Job 保存到 Google Datastore,它给了我这个错误

我在 DoFN 中的代码是

Datastore datastore= DatastoreOptions.getDefaultInstance().getService();
        TrackingRequest rq = gson.fromJson(c.element().toString(), TrackingRequest.class);
        Query<Entity> query = Query.entityQueryBuilder().kind("Post").filter(PropertyFilter.eq("postid", rq.postid))
                .build();
        QueryResults<Entity> posts = datastore.run(query);

        if (posts == null || !posts.hasNext()) {
            KeyFactory keyFactory = datastore.newKeyFactory().setKind("Post");
            Key key = keyFactory.newKey(rq.postid);

            Entity entity = Entity.newBuilder(key)
                    .set("appid", rq.appid)
                    .set("postid", rq.postid)
                    .set("title", rq.title)                 
                    .build();               


            datastore.put(entity);
            // c.output(((FullEntity<IncompleteKey>)entity).toPb());
        }

错误是:

exception: "java.lang.NoSuchMethodError: com.google.datastore.v1.Entity$Builder.putProperties(Ljava/lang/String;Lcom/google/datastore/v1/Value;)Lcom/google/datastore/v1/Entity$Builder;
at com.google.cloud.datastore.BaseEntity.toPb(BaseEntity.java:683)
at com.google.cloud.datastore.DatastoreImpl.put(DatastoreImpl.java:337)
at com.google.cloud.datastore.DatastoreHelper.put(DatastoreHelper.java:55)
at com.google.cloud.datastore.DatastoreImpl.put(DatastoreImpl.java:315)
at com.kryptonz.proccess.KryptonzArchive$RawToObjectConverter.processElement(KryptonzArchive.java:80)
at com.google.cloud.dataflow.sdk.util.SimpleDoFnRunner.invokeProcessElement(SimpleDoFnRunner.java:49)
at 

【问题讨论】:

标签: google-app-engine google-cloud-platform google-cloud-datastore google-cloud-dataflow


【解决方案1】:

看起来toPb 不是公共方法。提交了一个问题here

暂时你可以自己实现这个method

【讨论】:

    【解决方案2】:

    面临同样的问题,因此认为出路也可能对其他人有所帮助。

    我有google-cloud-datastore(版本0.11.2-beta)作为我的依赖项,它提出了datastore-v1-protos-1.0.1。而且我相信上述问题的根本原因是这个库特定的类com.google.datastore.v1.Entity.Builder,它既不支持putProperties,也不支持getPropertiesMap

    以更好的方式了解问题陈述,帮助我按照常规方式对问题进行分类。我必须确保获得一个新的 datastore-v1-protos 依赖项,它支持那些缺少的 API。因此,只需在我的 pom.xml 中添加以下依赖项

        <dependency>
            <groupId>com.google.cloud.datastore</groupId>
            <artifactId>datastore-v1-protos</artifactId>
            <version>1.3.0</version>
        </dependency>
    

    此更改可能会产生一些 protobuf 回归,因此请在您的 pom.xml 中包含以下依赖项以避免出现此类情况。

        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.2.0</version>
        </dependency>
    

    【讨论】:

    • 我认为这实际上不是他的问题,而是我的问题。谢谢。
    • 我很高兴.. 它帮助了某人
    【解决方案3】:

    检查您对 pom.xml 中数据存储的依赖关系以及您导入的库。

    <dependency>
         <groupId>com.google.cloud</groupId>
         <artifactId>google-cloud-datastore</artifactId>
         <version>1.90.0<</version>
      </dependency>
    
     import com.google.cloud.datastore.Entity;
     import com.google.cloud.datastore.Key;
    

    如果 req.post id 是数据存储实体中的 id/Name,那么您可以直接创建密钥并检查它是否存在于数据存储中。

    Datastore datastore =
      Datastore datastore= DatastoreOptions.getDefaultInstance().getService();
      Key key = datastore.newKeyFactory().setKind("Post").newKey(rq.postid);
      if(key == null){
            Entity entity = Entity.newBuilder(key)
                    .set("appid", rq.appid)
                    .set("postid", rq.postid)
                    .set("title", rq.title)                 
                    .build();   
              datastore.put(entity);   
    
      }
    

    【讨论】:

      猜你喜欢
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多