在dynamodb中将属性值更新为空白(“”)

时间:2012-03-30 08:45:10

标签: amazon-dynamodb

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

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

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

如果有的话,请建议我采取更好的方法。

提前致谢。

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

您也可以这样做

Table table = dynamoDB.getTable("myTable");
table.updateItem(new PrimaryKey("<MY HASH KEY NAME>", <MY HASH VALUE>), new AttributeUpdate("columnToRemove").delete());
相关问题