![](/img/trans.png)
[英]Google Cloud Dataflow ETL (Datastore -> Transform -> BigQuery)
[英]Saving to Google Datastore from Google Dataflow
我正在嘗試從 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-cloud-datastore
(版本0.11.2-beta
)作為我的依賴項,它提出了datastore-v1-protos-1.0.1
。 我相信上述問題的根本原因是這個庫特定類com.google.datastore.v1.Entity.Builder
,它既不支持putProperties
也不支持getPropertiesMap
。
以更好的方式了解問題陳述,幫助我按常規對問題進行排序。 我只需要確保獲得支持那些缺少的 API 的datastore-v1-protos
的新依賴項。 因此剛剛在我的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>
檢查 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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.