【发布时间】:2020-04-20 13:25:34
【问题描述】:
最近开始使用DynamoDB,我在通过多个键获取数据时遇到问题。
我正在尝试从一个表中获取多个项目。
我的表架构定义如下:
{
"AttributeDefinitions": [
{
"AttributeName": "id",
"AttributeType": "S"
},
{
"AttributeName": "date",
"AttributeType": "S"
}
],
"KeySchema": [
{
"AttributeName": "id",
"KeyType": "HASH"
},
{
"AttributeName": "date",
"KeyType": "RANGE"
}
],
...
}
我有一个 id 过滤器列表和每个 id 的日期范围:
[
{ "id": "abc", "start_date": "24/03/2020", "end_date": "26/03/2020" },
{ "id": "def", "start_date": "10/04/2020", "end_date": "20/04/2020" },
{ "id": "ghi", "start_date": "11/04/2020", "end_date": "11/04/2020" }
]
我需要获取与过滤列表匹配的所有项目。
问题是我不能使用Query 因为KeyConditionExpression 只接受一个分区键(我需要将它与整个过滤器列表匹配)
条件必须对单个分区键值执行相等测试。
我不能使用BatchGetItem,因为它需要确切的键(我需要一个日期范围作为排序键Key('date').between(start_date, end_date))
Keys - 定义表中特定项目的主键属性值数组。对于每个主键,您必须提供所有键属性。例如,使用简单的主键,您只需要提供分区键值。对于复合键,您必须同时提供分区键值和排序键值。
我有点迷茫... 有没有办法通过范围查询的多个键获取(通过单个请求 - 而不是来自循环的多个请求)?
您会建议更改表格吗?
【问题讨论】:
标签: python amazon-dynamodb boto3 dynamodb-queries