Database Reference
In-Depth Information
GetItemResult getItemResult =
dynamoDBClient.getItem(getItemRequest);
Update item
The update item method from DynamoDB client is quite a useful method that allows us to
do multiple things in one go as follows:
• Modify existing value of an attribute
• Add a new attribute to an existing set
• Delete an attribute from an existing set
If you invoke the updateItem method for a non-existing item, then DynamoDB adds
the new item to the table. You can also use the AttributeAction.ADD action to add
a value to the existing set of values and do addition and subtraction for numeric values.
The following is the syntax to use the updateItem method:
// Create Hash Map of item with attributes to be updated.
Map<String, AttributeValueUpdate> updateItems = new
HashMap<String, AttributeValueUpdate>();
// Add two new authors to the list.
updateItems.put("authors", new AttributeValueUpdate()
.withAction(AttributeAction.ADD)
.withValue(new AttributeValue().withSS("XYZ", "PQR")));
// Hash Map of key
HashMap<String, AttributeValue> primaryKey = new
HashMap<String, AttributeValue>();
primaryKey.put("bookId", new
AttributeValue().withN("2001"));
// To increase the no. of chapters of the topic by 2
updateItems.put("chapters", new AttributeValueUpdate()
.withAction(AttributeAction.ADD)
.withValue(new AttributeValue().withN("2")));
// To delete an attribute called "rating"
updateItems.put("rating", new AttributeValueUpdate()
.withAction(AttributeAction.DELETE));
Search WWH ::




Custom Search