I need to perform a batch update to dynamo db in java. By update I mean that I have the primary key of an item and want to update a single attribute of the item. I tried first getting the items through batchGetItem, modifying it and performing a batchWriteItem. Even batchSave of dynamoDb mapper doesn't provide updates. I can use this method, but is there a way by which I can perform batchUpdate on the items directly without getting the whole item from the db ?
Batch Update in DynamoDB
1.1k Views Asked by sahillearner At
2
There are 2 best solutions below
0
On
You can try the new PartiQl BatchExecute Statement.
https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchExecuteStatement.html
The syntax is very Sql like, from the above docs ...
[
{
"Statement": "INSERT INTO Music value {'Artist':'?','SongTitle':'?'}",
"Parameters": [{"S": "Acme Band"}, {"S": "Best Song"}]
},
{
"Statement": "UPDATE Music SET AwardsWon=1 SET AwardDetail={'Grammys':[2020, 2018]} where Artist='Acme Band' and SongTitle='PartiQL Rocks'"
}
]
Based on Amazon documentation it is not possible, they ask to use
UpdateItem: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.htmlSo you can either use your approach or execute parallel
UpdateItemin your application, at the end, as stated by Amazon docs, the benefits of using BatchWriteItem are mainly latency costs and complexity:Your approach may be faster (less latency) but a higher cost (need to read and write the whole item). Meanwhile
UpdateItemapproach may be more complex to implement depending on the technology you use.