使用Google Cloud Dataflow删除或更新数据存储区属性

时间:2022-08-01 15:23:38

If you want to add a property to an existing entity, you just "clone it" and add it.

如果要将属性添加到现有实体,只需“克隆”并添加即可。

  Entity oldEntity = c.element();
  Entity.Builder entityBuilder = Entity.newBuilder(oldEntity);
  entityBuilder.addProperty(
          DatastoreHelper.makeProperty("newProperty",
                  DatastoreHelper.makeValue("Value")
          )
  );
  c.output(entityBuilder.build());

If you want to update a property, adding it again hoping to overwrite the old value won't work. It won't save to the datastore because a property's name must be unique, and now you'd have two with the same name.

如果要更新属性,再次添加它希望覆盖旧值将不起作用。它不会保存到数据存储区,因为属性的名称必须是唯一的,现在您有两个具有相同名称的名称。

Error writing to the Datastore (400): Entity has duplicate property name

If you want to remove a property, you need to know the index from the Property List, and for that you'd need to list all the properties, check if the property you want to update exists, keep track of the index number, and then remove it.

如果要删除属性,则需要知道属性列表中的索引,为此您需要列出所有属性,检查是否存在要更新的属性,跟踪索引号,以及然后删除它。

Is there a builtin helper for this procedure or a shortcut I'm missing?

这个程序是否有内置助手或我缺少的快捷方式?

1 个解决方案

#1


0  

Currently Google Cloud Dataflow's Java SDK uses Datastore API v1beta2 and there's no way to directly add a property to an entity, not even using DatastoreHelper.getPropertyMap and adding properties to the resulting Map<String, Value> because that method returns an UnmodifiableMap.

目前,Google Cloud Dataflow的Java SDK使用数据存储API v1beta2,并且无法直接向实体添加属性,甚至无法使用DatastoreHelper.getPropertyMap并将属性添加到生成的Map ,因为该方法返回UnmodifiableMap。 ,value>

When they switch to v1beta3 the properties will be exposed as just a map, according to a team member.

根据团队成员的说法,当他们切换到v1beta3时,属性将仅作为地图公开。

So this is how I managed it on v1beta2:

所以这就是我在v1beta2上管理它的方式:

Entity oldEntity = c.element();

// We need to get the property map, but the one from DatastoreHelper is an unmodifiableMap
Map<String, Value> oldEntity_map = DatastoreHelper.getPropertyMap(oldEntity);
Map<String, Value> newEntity_map = new HashMap<String, Value>();
newEntity_map.putAll(oldEntity_map);

// Adding or updating a property
newEntity_map.put("newProperty", DatastoreHelper.makeValue("Value").build());
// Deleting a property
newEntity_map.remove("delete-this");

Entity.Builder updatedEntity = Entity.newBuilder(oldEntity);
updatedEntity.clear();
updatedEntity.setKey(oldEntity.getKey());

for (Map.Entry<String, Value> property : newEntity_map.entrySet())
{
    updatedEntity.addProperty(
       DatastoreHelper.makeProperty(property.getKey(), property.getValue()));
}

c.output(updatedEntity.build());

#1


0  

Currently Google Cloud Dataflow's Java SDK uses Datastore API v1beta2 and there's no way to directly add a property to an entity, not even using DatastoreHelper.getPropertyMap and adding properties to the resulting Map<String, Value> because that method returns an UnmodifiableMap.

目前,Google Cloud Dataflow的Java SDK使用数据存储API v1beta2,并且无法直接向实体添加属性,甚至无法使用DatastoreHelper.getPropertyMap并将属性添加到生成的Map ,因为该方法返回UnmodifiableMap。 ,value>

When they switch to v1beta3 the properties will be exposed as just a map, according to a team member.

根据团队成员的说法,当他们切换到v1beta3时,属性将仅作为地图公开。

So this is how I managed it on v1beta2:

所以这就是我在v1beta2上管理它的方式:

Entity oldEntity = c.element();

// We need to get the property map, but the one from DatastoreHelper is an unmodifiableMap
Map<String, Value> oldEntity_map = DatastoreHelper.getPropertyMap(oldEntity);
Map<String, Value> newEntity_map = new HashMap<String, Value>();
newEntity_map.putAll(oldEntity_map);

// Adding or updating a property
newEntity_map.put("newProperty", DatastoreHelper.makeValue("Value").build());
// Deleting a property
newEntity_map.remove("delete-this");

Entity.Builder updatedEntity = Entity.newBuilder(oldEntity);
updatedEntity.clear();
updatedEntity.setKey(oldEntity.getKey());

for (Map.Entry<String, Value> property : newEntity_map.entrySet())
{
    updatedEntity.addProperty(
       DatastoreHelper.makeProperty(property.getKey(), property.getValue()));
}

c.output(updatedEntity.build());