[英]Querying a DynamoDb table using a combination of `begins_with` and `between` conditions
How can I query a DynamoDb table using a combination of the BETWEEN
and BEGINS_WITH
?如何使用
BETWEEN
和BEGINS_WITH
的组合查询 DynamoDb 表?
A simplified example of my tables partition and sort key looks like this:我的表分区和排序键的简化示例如下所示:
PK |SK
my_pk |2022-05-13T08:30:00Z#tag0
my_pk |2022-05-13T08:45:00Z#tag0
my_pk |2022-05-13T08:45:00Z#tag1
my_pk |2022-05-13T08:45:00Z#tag2
my_pk |2022-05-13T09:00:00Z#tag0
my_pk |2022-05-13T09:00:00Z#tag1
If my SK
didn't include the ...#tag
postfix I could use a KeyConditionExpression
like this:如果我的
SK
不包含...#tag
后缀,我可以使用这样的KeyConditionExpression
:
"KeyConditionExpression": "#pk = :pk AND #sk BETWEEN :start AND :end"
where start
and end
are eg 2022-05-13T08:45:00Z
and 2022-05-13T09:00:00Z
, respectively, but I am unsure how to do it when the postfix is there.其中
start
和end
分别是2022-05-13T08:45:00Z
和2022-05-13T09:00:00Z
,但是当后缀存在时我不确定该怎么做。
Note: I never need to query with the tag
, it's just there to ensure that the composite primary key is unique.注意:我从不需要使用
tag
进行查询,它只是为了确保复合主键是唯一的。
Note2: I am using python and boto3 to execute the query.注2:我使用python和boto3来执行查询。
You can use between where :end
= 2022-05-13T09:00:00Z$
.您可以在 where
:end
= 2022-05-13T09:00:00Z$
之间使用。 Dollar sign is the next value after hash in ASCII, so this will capture all values starting with 2022-05-13T09:00:00Z#
.美元符号是 ASCII 中哈希后的下一个值,因此这将捕获以
2022-05-13T09:00:00Z#
开头的所有值。
Partition and sort keys in a LSI or GSI do not need to be unique. LSI 或 GSI 中的分区键和排序键不需要是唯一的。 So you can create an LSI, and use date column there as sort key.
所以你可以创建一个 LSI,并在那里使用日期列作为排序键。
This post exactly talks about this - Local Secondary Indexes .这篇文章正是在谈论这个 - 本地二级索引。
Here is an screenshot from that page.这是该页面的屏幕截图。
To ensure uniqueness in composite primary key, you can store some random string in
sort key
column.为了确保复合主键的唯一性,您可以在
sort key
列中存储一些随机字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.