python - 如何将变量正确传递到这个 boto3 batch_get_item 函数中?

标签 python boto3

这里可以看到一个关于如何为batch_get_item传递硬编码值的小例子

Is there a Python API for submitting batch get requests to AWS DynamoDB?

文档在这里

http://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html#DynamoDB.Client.batch_get_item

但我简直不敢相信这就是经验丰富的 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/

相关文章:

python - 使用 Python 以正确的顺序查找一组模式的正则表达式出现

python - 带有坐标的推文 Tweepy python

python - Boto3 Kinesis Video GetMedia 和 OpenCV

python - 使用 boto 时,长时间运行的客户端对象是否安全?

python - 使用 Boto3 列出所有 "Active"EMR 集群

amazon-web-services - 创建 A 记录时出现 NoSuchHostedZone 错误

python - 如何将方法存储在静态变量中?

python - 使用 Matplotlib 在 Python 中填充曲线和 x 轴之间的区域

python - GDB Python API : Doesn't . parse_and_eval() 使 .cast() 和 .dereference() 变得多余?

python-3.x - 模拟 : Client does not have the attribute 'get_object'