简体   繁体   中英

DynamoDB Batch Update

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?

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

DynamoDB API operations list

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

DynamoDBMapper.batchSave(Iterable<? extends Object> objectsToSave)我使用DynamoDBMapper.batchSave(Iterable<? extends Object> objectsToSave)

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

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

我使用了一个dynamoDB更新触发器,然后我做了一个模板,告诉我我应该修改哪些项目,我把它们放在一个队列中,然后它们读取另一个队列中的消息以一一更新

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM