dynamodb都用gt和begins_with查询sort键吗?
我有一个单一的桌子设计,其中有聊天室(PK),并带有时间戳消息(SK)。由于它是一个表设计,因此SK具有msg#
前缀,然后是消息创建时间戳,以使消息实体与其他实体分开。
我想在一定的时间戳后检索所有消息。似乎关键条件应为pk =“< chatroomid>” and begins_with(sk,“ msg#”)和sk gt“ msg#< lastread>”
。 SK条件的第一部分是仅获取消息实体,第二部分是仅获取新消息。这样的排序键是否有可能具有双重条件?似乎应该有可能,因为它表示一系列连续的键。
I have a single table design where I have chat rooms (PK) with timestamped messages (SK). Since it's a single table design the SK has a MSG#
prefix, followed by the message creation timestamp, to keep message entities separate from other entities.
I'd like to retrieve all messages after a certain timestamp. It seems like the key condition should be PK = "<ChatRoomId>" AND begins_with(SK, "MSG#") AND SK GT "MSG#<LastRead>"
. The first part of the SK condition is to only fetch message entities and the second is to only fetch new messages. Is it possible to have a double conditions on the sort key like this? It seems like it should be possible as it denotes a contiguous range of sort keys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过使用之间轻松实现这一目标:
这样,您将以
&lt; yourdate&gt;
的方式获得所有消息,并且没有其他前缀的记录。除非您计划非常,否则这将起作用。You can easily achieve that by using between:
This way you will get all messages starting at
<YourDate>
and no records with other prefixes. This will work unless you're planning very far ahead.我有完全相同的用例,并找到了这个答案,感谢此建议,它有效,但我们决定进一步研究 - “之间”是包容性的,我们必须浪费一个阅读能力单位或构成一个假价值作为解决方法。
事实证明,DynamoDB API提供了此功能,它是独家开始键: https://docs.aws.amazon.com/amazondynamodb/latest/apireference/api_query.html#dddb-query-requery-requert-request-exclusivestartkey
公认的文档并不令人鼓舞似乎表明该参数是一些不透明的数据,您只能通过以前的查询来获得:
但是该键的实际内容非常简单且透明:它是
{“ pk”:{“ s”:“ your_pk”},“ sk”:{“ s”:“ exprivey_start_sk”}} < /code>(用实际键替换PK/SK-如果您进行单个表设计,则可能使用这些通用名称)。如果您要查询GSI而不是主表,则可以提供GSIPK/GSISK。您可以执行一些手动查询,并观察返回的LastEvaLEDKEY以验证其预期。
从那里,您可以将oreater_than和begins_with结合起来,大_than表示为分页参数
I have exactly the same use case and found out this answer, thanks for this suggestion, it works but we decided to research further - "between" is inclusive and we'd have to either waste one read capacity unit or make up a fake value as a workaround.
Turns out, the DynamoDB API provides this feature, it's the exclusive start key: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html#DDB-Query-request-ExclusiveStartKey
Admittedly, the documentation is not very encouraging and seems to suggest that the parameter is some opaque data that you can only obtain by having a previous query:
But the actual content of that key is very simple and transparent: it's a map like
{"PK": {"S": "your_pk"}, "SK": {"S": "exclusive_start_sk"}}
( replace PK/SK with your actual key - if you're doing single table design you're probably using those generic names ). GSIPK/GSISK may be provided instead, if you're querying a GSI instead of the main table. You can do some manual query and observe the returned LastEvaluatedKey to verify what it's expecting.From there you can combine greater_than and begins_with, greater_than being expressed as a pagination parameter