【发布时间】:2021-12-21 22:10:28
【问题描述】:
我在 DynamoDB 中有一个表,其中包含分区键和排序键。我想检索具有给定分区键的所有项目,而不考虑排序键。
我该怎么做?
【问题讨论】:
标签: python-3.x amazon-dynamodb boto3
我在 DynamoDB 中有一个表,其中包含分区键和排序键。我想检索具有给定分区键的所有项目,而不考虑排序键。
我该怎么做?
【问题讨论】:
标签: python-3.x amazon-dynamodb boto3
以下方法既适用于仅具有分区键的表,也适用于具有分区键和排序键的表:
from boto3 import resource
from boto3.dynamodb.conditions import Key
dynamodb_resource = resource('dynamodb')
def query_table(table_name, key=None, value=None):
table = dynamodb_resource.Table(table_name)
if key is not None and value is not None:
filtering_exp = Key(key).eq(value)
return table.query(KeyConditionExpression=filtering_exp)
raise ValueError('Parameters missing or invalid')
if __name__ == '__main__':
resp = query_table(
table_name='my-table',
key='key-name',
value='match-me'
)
items = resp.get('Items')
print(len(items))
注意:我最初为 here 找到了一个有用的答案。信用到期! (链接更新于 8/21)
【讨论】: