Batch Update in DynamoDB

1k views Asked by At

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 ?

2

There are 2 answers

0
sas1138 On

You can try the new PartiQl BatchExecute Statement.

https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchExecuteStatement.html

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ql-reference.multiplestatements.batching.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'"
}
]
0
Alberto Martin On

Based on Amazon documentation it is not possible, they ask to use UpdateItem: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html

So you can either use your approach or execute parallel UpdateItem in your application, at the end, as stated by Amazon docs, the benefits of using BatchWriteItem are mainly latency costs and complexity:

BatchWriteItem performs the specified put and delete operations in parallel, giving you the power of the thread pool approach without having to introduce complexity into your application.

Your approach may be faster (less latency) but a higher cost (need to read and write the whole item). Meanwhile UpdateItem approach may be more complex to implement depending on the technology you use.