简体   繁体   中英

Saving to Google Datastore from Google Dataflow

I am trying to save to Google Datastore from Google Dataflow Job it gives me this error

My Code inside the DoFN is

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());
        }

The error is :

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 

Looks like toPb is not a public method. Filed an issue here .

For the time being you could implement this method yourself.

Faced the same issue and so thought the way out might help others too.

I had google-cloud-datastore (version 0.11.2-beta ) as my dependency which come up with datastore-v1-protos-1.0.1 . And I believe the root cause for the mentioned problem was this libs specific class com.google.datastore.v1.Entity.Builder , which didn't support neither putProperties nor getPropertiesMap .

Knowing the problem statement in a better way, helped me to sort the issue conventionally. I had to just make sure to get a new dependency of datastore-v1-protos which supports those missing APIs. Hence just added below dependency in my pom.xml

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

This change might create some protobuf regression, so include below dependency in your pom.xml to avoid, if there is any such.

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

Check your dependency of datastore in pom.xml and the library you import.

<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;

And if the req.post id is the id/Name in the datastore entity then you can directly create key and check if it present in the datastore.

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);   

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM