这里可以看到一个关于如何为batch_get_item传递硬编码值的小例子
Is there a Python API for submitting batch get requests to AWS DynamoDB?
文档在这里
但我简直不敢相信这就是经验丰富的 python 用户将 key 传递给函数的方式。是否可以将字符串 (ids) 列表传递到 Keys 数组中?这是如何完成的?
最佳答案
您可以使用列表理解来创建需要作为 Keys
参数传递给 batch_get_item
的值。像这样的事情会起作用:
# assume ids is a list of strings representing the ids you want to retrieve
# also assume that the primary key is called primary_key
keys = [{'primary_key': {'S': i}} for i in ids]
db_client.batch_get_item(
RequestItems = {
"my_table": {
"Keys": keys
}
}
)
如果 keys
的值为 ['foo', 'bar', 'fie', 'baz']
keys
的值> 在上面的代码中将是:
[{'primary_key': {'S': 'foo'}},
{'primary_key': {'S': 'bar'}},
{'primary_key': {'S': 'fie'}},
{'primary_key': {'S': 'baz'}}]
关于python - 如何将变量正确传递到这个 boto3 batch_get_item 函数中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42685656/