user9438495
user9438495

Reputation:

Can a single boto3 dynamodb client update multiple tables simultaneously?

client = boto3.resource('dynamodb')

I would like to know if I am able to use batchwriteitem to 2 tables with this client simultaneously, or should I create another client? Can dynamodb even handle simultaneous updates for different tables?

Upvotes: 0

Views: 810

Answers (1)

Yes, you can use the same client to write or delete items in two or more tables at the same time, docs

The BatchWriteItem operation puts or deletes multiple items in one or more tables. A single call to BatchWriteItem can write up to 16 MB of data, which can comprise as many as 25 put or delete requests. Individual items to be written can be as large as 400 KB

import boto3

client = boto3.client("dynamodb")

response = client.batch_write_item(
    RequestItems={
        "table_1": [
            {
                "PutRequest": {
                    "Item": {
                        "Key": {
                            "S": "key1",
                        }
                    }
                },
            },
            {
                "PutRequest": {
                    "Item": {
                        "Key": {
                            "S": "key2",
                        }
                    }
                },
            },
        ],
        "table_2": [
            {
                "PutRequest": {
                    "Item": {
                        "Key": {
                            "S": "key1",
                        }
                    }
                },
            },
            {
                "PutRequest": {
                    "Item": {
                        "Key": {
                            "S": "key2",
                        }
                    }
                },
            },
        ],
    },
    ReturnConsumedCapacity="TOTAL",
)

# output
{'UnprocessedItems': {}, 'ConsumedCapacity': [{'TableName': 'table_1', 'CapacityUnits': 2.0}, {'TableName': 'table_2', 'CapacityUnits': 2.0}], 'ResponseMetadata': {'RequestId': 'XXX', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Fri, 06 Aug 2021 01:53:43 GMT', 'content-type': 'application/x-amz-json-1.0', 'content-length': '132', 'connection': 'keep-alive', 'x-amzn-requestid': 'XXX', 'x-amz-crc32': '123'}, 'RetryAttempts': 0}}

Upvotes: 1

Related Questions