4

How can one update an attribute value in DynamoDB table to ""?

one option is to get the item into a document and then copy all the attributes into a new document except the attribute whose value is to be updated to "" and then call putitem, this would basically replace the whole item (as the hash key exists) and since I don't have the attribute anymore it would be deleted.

Note: I could have simply done delete item but my requirement is to update more than one attribute value and one of these is blank.

Please suggest me a better approach if any.

Thanks in advance.

2 Answers 2

6

DynamoDB allows update in a existent row, using the UpdateItemRequest(Java SDK).

Like the following example:

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
  • 2
    Thanks a lot, i have also figured out that if i use Document model and set a attribute value to be NULL or "", then the AWS SDK shall do the delete operation for me. Mar 31, 2012 at 10:11
1

You can also do something like this

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.