因此,我有一个带有主分区键列 foo_id
且没有主排序键的 dynamodb 表。我有一个 foo_id
值列表,并希望获得与此 ID 列表相关的观察结果。
我认为最好的方法(?)是使用 batch_get_item()
,但它不适合我。
# python code
import boto3
client = boto3.client('dynamodb')
# ppk_values = list of `foo_id` values (strings) (< 100 in this example)
x = client.batch_get_item(
RequestItems={
'my_table_name':
{'Keys': [{'foo_id': {'SS': [id for id in ppk_values]}}]}
})
我正在使用 SS
因为我正在传递一个字符串列表(foo_id
值列表),但我得到:
ClientError: An error occurred (ValidationException) when calling the
BatchGetItem operation: The provided key element does not match the
schema
所以我假设这意味着它认为 foo_id
包含列表值而不是字符串值,这是错误的。
--> 这个解释对吗?批量查询一组主分区键值的最佳方法是什么?
最佳答案
Boto3 现在有一个 batch_get_item
版本,可以让您以更自然的 Pythonic 方式传递 key ,而无需指定类型。
您可以在 https://github.com/awsdocs/aws-doc-sdk-examples 中找到完整且有效的代码示例.该示例处理了重试的一些额外细微差别,但这里是回答该问题的代码部分的摘要:
import logging
import boto3
dynamodb = boto3.resource('dynamodb')
logger = logging.getLogger(__name__)
movie_table = dynamodb.Table('Movies')
actor_table = dyanmodb.Table('Actors')
batch_keys = {
movie_table.name: {
'Keys': [{'year': movie[0], 'title': movie[1]} for movie in movie_list]
},
actor_table.name: {
'Keys': [{'name': actor} for actor in actor_list]
}
}
response = dynamodb.batch_get_item(RequestItems=batch_keys)
for response_table, response_items in response.items():
logger.info("Got %s items from %s.", len(response_items), response_table)
关于python - 如何在给定主分区键值列表的情况下一次 batch_get_item 多个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42076424/