使用Google Cloud Dataflow的Java API for Datastore将属性设置为null?

时间:2022-12-12 15:23:19

The com.google.cloud.datastore package mentioned in most of Google's Java documentation for Datastore provides a Builder class with a setNull method used to set any property value to null, e.g.

大多数Google的Datastore Java文档中提到的com.google.cloud.datastore包为Builder类提供了一个setNull方法,用于将任何属性值设置为null,例如:

FullEntity.Builder<IncompleteKey> builder = FullEntity.newBuilder();
builder.setNull("propertyName");

Google Cloud Dataflow/Apache Beam's DatastoreIO class requires Entities in the package com.google.datastore.v1, and the builder methods do not include a similar setNull method. How do I set a property to null using the V1 Java APIs?

Google Cloud Dataflow / Apache Beam的DatastoreIO类需要com.google.datastore.v1包中的实体,并且构建器方法不包含类似的setNull方法。如何使用V1 Java API将属性设置为null?

1 个解决方案

#1


3  

If using the com.google.datastore.v1 package Entity and Value, this is how to set a null value (with Apache Beam/Google Dataflow):

如果使用com.google.datastore.v1包实体和值,则使用此方法设置空值(使用Apache Beam / Google Dataflow):

import com.google.datastore.v1.Entity;
import com.google.datastore.v1.Entity.Builder;
import com.google.datastore.v1.Value;
import com.google.protobuf.NullValue;

Value nullValue = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
Entity.Builder builder = Entity.newBuilder();
builder.putProperties("propertyName", nullValue);

(Answering my own question since it took me quite a while to figure this out!)

(回答我自己的问题,因为我花了很长时间来解决这个问题!)

#1


3  

If using the com.google.datastore.v1 package Entity and Value, this is how to set a null value (with Apache Beam/Google Dataflow):

如果使用com.google.datastore.v1包实体和值,则使用此方法设置空值(使用Apache Beam / Google Dataflow):

import com.google.datastore.v1.Entity;
import com.google.datastore.v1.Entity.Builder;
import com.google.datastore.v1.Value;
import com.google.protobuf.NullValue;

Value nullValue = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
Entity.Builder builder = Entity.newBuilder();
builder.putProperties("propertyName", nullValue);

(Answering my own question since it took me quite a while to figure this out!)

(回答我自己的问题,因为我花了很长时间来解决这个问题!)