4

如何将 DynamoDB 表中的属性值更新为“”?

一种选择是将项目放入文档中,然后将所有属性复制到新文档中,除了要更新为“”的属性,然后调用 putitem,这基本上会替换整个项目(因为哈希键存在) 并且由于我不再拥有该属性,因此它将被删除。

注意:我可以简单地删除项目,但我的要求是更新多个属性值,其中一个是空白的。

如果有的话,请给我建议一个更好的方法。

提前致谢。

4

2 回答 2

6

DynamoDB 允许使用 UpdateItemRequest(Java SDK) 更新现有行。

像下面的例子:

Map<String, AttributeValueUpdate> updateItems = new HashMap<String, AttributeValueUpdate>();

updateItems.put("columnToRemove", new AttributeValueUpdate()
                                      .withAction(AttributeAction.DELETE));

updateItems.put("columnToRemove2", new AttributeValueUpdate()
                                      .withAction(AttributeAction.DELETE));

UpdateItemRequest updateItemRequest = new UpdateItemRequest()
                                          .withTableName(tableName)
                                          .withKey(itemKey)
                                          .withAttributeUpdates(updateItems);

UpdateItemResult result = client.updateItem(updateItemRequest);
于 2012-03-30T20:02:45.767 回答
1

你也可以做这样的事情

Table table = dynamoDB.getTable("myTable");
table.updateItem(new PrimaryKey("<MY HASH KEY NAME>", <MY HASH VALUE>), new AttributeUpdate("columnToRemove").delete());
于 2015-09-14T13:34:49.517 回答