Update attribute value to empty ("") in dynamodb

How can I update the value of an attribute in a DynamoDB table to ""?

one parameter is to get the element in the document and then copy all the attributes into the new document except the attribute whose value needs to be updated to "" and then call putitem, this will basically replace the whole element (like hash key), and since I no longer have the attribute, it will be removed.

Note. I could just delete the delete element, but my requirement is to update more than one attribute value and one of them is empty.

Please suggest me the best approach, if any.

Thanks in advance.

+3


source to share


2 answers


DynamoDB allows you to update an existing row using UpdateItemRequest (Java SDK).

As in 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);

      

+4


source


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());

      

0


source







All Articles