Reputation: 121
I have a java application which is building a DynamoDB client write request as
WriteRequest.builder().putRequest(PutRequest.builder().item(attributeValueMap).build()).build();
The above request is replacing the items with same PartitionKey and SortKey instead of upserting the data into the table. Any idea what am I doing wrong or do I need a to pass any additional parameter in PutRequest ?
Upvotes: 1
Views: 383
Reputation: 8515
As the commenter mentions, you want to use UpdateItem if you want to "patch" an item. PutItem will replace the entire item. You can read more about the differences here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithItems.html#WorkingWithItems.WritingData
This is a simplified code sample for how that works in Javav2 SDK.
HashMap<String,AttributeValue> itemKey = new HashMap<>();
itemKey.put(key, AttributeValue.builder()
.s(keyVal)
.build());
HashMap<String,AttributeValueUpdate> updatedValues = new HashMap<>();
// Put your attributes/values you wish to update here.
// Attributes you don't include won't be effected by the update
updatedValues.put(name, AttributeValueUpdate.builder()
.value(AttributeValue.builder().s(updateVal).build())
.action(AttributeAction.PUT)
.build());
UpdateItemRequest request = UpdateItemRequest.builder()
.tableName(tableName)
.key(itemKey)
.attributeUpdates(updatedValues)
.build();
ddb.updateItem(request);
Upvotes: 2