73

Is there any API in DynamoDB to update a batch of items? There is an API to write new items in batches (BatchWriteItem) and update single item using UpdateItem, but is it possible to update multiple items in one call?

Ninjakannon
  • 3,751
  • 7
  • 53
  • 76
user1846749
  • 2,165
  • 3
  • 23
  • 36

9 Answers9

49

There is no batch update item API available in DynamoDB at the moment.

DynamoDB API operations list

notionquest
  • 37,595
  • 6
  • 111
  • 105
30

I know this is an old question by now, but DynamoDB recently added a Transaction api which supports update:

Update — Initiates an UpdateItem operation to edit an existing item's attributes or add a new item to the table if it does not already exist. Use this action to add, delete, or update attributes on an existing item conditionally or without a condition.

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/transaction-apis.html

Chris Barrett
  • 346
  • 3
  • 4
  • Do you know if we might be able to provide the flag UPDATE_SKIP_NULL_ATTRIBUTES while calling the update or does it removes values of all attributes that are null? – Arpit May 05 '20 at 04:48
  • 7
    This works but for whoever using this be mindful of transaction costing double the normal update. – Wildhammer Jul 23 '20 at 21:36
  • Worth noting: Transactions don't allow for Return Values. In my use case where what I wanted to do is increment some counters - this would require at least 2 operations transact write + transact read - which is not ideal. – Myxoh Apr 14 '23 at 14:36
9

I reached this thread on similar query, hope this might help.

DynamoDB supports Batch Statement Execution which is described in documentation. This works with client object rather than resource object. Then I used the PartiQL update statement supported by DynamoDB and described here.

Python code reference looks something like this:

client = boto3.client('dynamodb')

batch = ["UPDATE users SET active='N' WHERE email='<user_email>' RETURNING [ALL|MODIFIED] [NEW|OLD] *;", "UPDATE users ..."]  # Limit to 25 per batch
request_items = [{'Statement': _stat} for _stat in batch]
batch_response = client.batch_execute_statement(Statements=request_items)

This is minimal code. You can use multi-threading to execute multiple batches at once.

ap14
  • 4,393
  • 1
  • 15
  • 30
6

With PartiQL you can execute batch insert and update just like SQL.

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ql-reference.multiplestatements.batching.html

Sajed
  • 61
  • 1
  • 1
  • 4
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 28 '21 at 08:12
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30197765) – Tyler2P Oct 29 '21 at 00:41
2

BatchWriteItem cannot update items. To update items, use the UpdateItem action. BatchWriteItem operation puts or deletes multiple items in one or more tables

Reference: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html

2

I use DynamoDBMapper.batchSave(Iterable<? extends Object> objectsToSave) for this purpose.

Marian
  • 387
  • 3
  • 17
  • 4
    from https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.Methods.html#DynamoDBMapper.Methods.batchSave batchSave Saves objects to one or more tables using one or more calls to the AmazonDynamoDB.batchWriteItem method. This method does not provide transaction guarantees. – bagi Oct 04 '18 at 23:33
2

No there is no batch update currently , you can use a single update Item call and have a workflow over it like AWS SWF or AWS step functions

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html

swarnim gupta
  • 213
  • 1
  • 5
  • There is also a batch update capability when the updates are made using PartiQL, https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#batchExecuteStatement-property This apparently allows up to 25 single items to be updated Also, https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ql-reference.update.html – Marvin Sep 21 '21 at 13:24
0

I use a dynamoDB update trigger, then I made a template that said to me what items I should modify, I put them on a queue and them read queue messages in other to update one by one

0

Not exactly a batch delete but I did this in a python lambda function just now:

    import json
    import boto3

    client = boto3.client('dynamodb')

    def lambda_handler(event, context):
    
    idList = [
        "id1",
        "id2
        ...
        "id100",
    ]

    for itemID in idList:
        test = client.update_item(
                TableName='Your-Table-Name',
                Key={
                    'id': {
                        'S': itemID
                    }
                },
                UpdateExpression="set exressionToChange=:r",
                ExpressionAttributeValues={
                        ':r': {'S':'New_Value'}},
                ReturnValues="UPDATED_NEW")
        
    
    return

To get the idList, I downloaded the values in a CSV, copied them into VSCode and then did a find and replace with regex (CMD-F then click .\*) and set find to ".*" and replace to "$0", which basically replaces every line with itself in quotes and a comma

So basically before:

    id1
    id2
    id3
    ...

And after

    "id1",
    "id2",
    "id3",
    ...

Just replace "idList = [...]" with your ids, "Your-Table-Name", "expressionToChange" and lastly, "New_Value".

Also you will have give your lambda function permission to "Update Item" in DynamoDB or you will get an error

logi-kal
  • 7,107
  • 6
  • 31
  • 43