dynamodb都用gt和begins_with查询sort键吗?

发布于 2025-02-05 20:16:45 字数 307 浏览 1 评论 0原文

我有一个单一的桌子设计,其中有聊天室(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

上课铃就是安魂曲 2025-02-12 20:16:45

您可以通过使用之间轻松实现这一目标:

PK = "<ChatRoomId>" AND SK BETWEEN "MSG#<YourDate>" AND "MSG#9999-99-99"

这样,您将以&lt; yourdate&gt;的方式获得所有消息,并且没有其他前缀的记录。除非您计划非常,否则这将起作用。

You can easily achieve that by using between:

PK = "<ChatRoomId>" AND SK BETWEEN "MSG#<YourDate>" AND "MSG#9999-99-99"

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.

梨涡 2025-02-12 20:16:45

我有完全相同的用例,并找到了这个答案,感谢此建议,它有效,但我们决定进一步研究 - “之间”是包容性的,我们必须浪费一个阅读能力单位或构成一个假价值作为解决方法。

事实证明,DynamoDB API提供了此功能,它是独家开始键: https://docs.aws.amazon.com/amazondynamodb/latest/apireference/api_query.html#dddb-query-requery-requert-request-exclusivestartkey

公认的文档并不令人鼓舞似乎表明该参数是一些不透明的数据,您只能通过以前的查询来获得:

该操作将评估的第一个项目的主要键。使用上一个操作中lastEvaletekey返回的值。

但是该键的实际内容非常简单且透明:它是{“ 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:

The primary key of the first item that this operation will evaluate. Use the value that was returned for LastEvaluatedKey in the previous operation.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文