单个 boto3 dynamodb 客户端可以同时更新多个表吗?
Can a single boto3 dynamodb client update multiple tables simultaneously?
client = boto3.resource('dynamodb')
我想知道我是否可以通过此客户端同时对 2 个表使用 batchwriteitem,还是应该创建另一个客户端? dynamodb 甚至可以处理不同表的同时更新吗?
是的,您可以使用同一个客户端同时写入或删除两个或多个表中的项目,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}}
client = boto3.resource('dynamodb')
我想知道我是否可以通过此客户端同时对 2 个表使用 batchwriteitem,还是应该创建另一个客户端? dynamodb 甚至可以处理不同表的同时更新吗?
是的,您可以使用同一个客户端同时写入或删除两个或多个表中的项目,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}}