gpt4 book ai didi

nosql - DynamoDB 邻接列表主键

转载 作者:行者123 更新时间:2023-12-02 11:24:31 26 4
gpt4 key购买 nike

我正在使用 DynamoDB 完成一个练习来模拟多对多关系。我需要允许帖子和标签之间存在多对多关系。每个帖子可以有多个标签,每个标签可以有多个帖子。

我在 id 上有一个主键和 type 上的主排序键然后是 id 上的另一个全局索引和 data , 我在 id 上添加了另一个全局索引和 type再次,但我认为这是多余的。

这是我到目前为止所拥有的。

id(Partition key)      type(Sort Key)       target       data
------------- ---------- ------ ------
1 post 1 cool post
tag tag tag n/a
1 tag tag orange

---------------------------------------------
---- inserting another tag will overwrite ---
---------------------------------------------

1 tag tag green

我正在从这个很棒的演讲中获得建议 https://www.youtube.com/watch?v=jzeKPKpucS0而这些不是那么棒的文档 https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-adjacency-graphs.html

我遇到的问题是,如果我尝试添加另一个带有 id 的标签“1”和 type “标记”它将覆盖现有标记,因为它将具有相同的复合键。我在这里缺少什么?似乎建议将主键和排序键设为 idtype .我应该让我的类型更像“tag#orange”吗?在那种情况下,我可以在 target 上放置一个全局索引。在类型上带有排序键。通过这种方式,我可以通过查询 target = "tag"并键入以 "tag"开头来获取具有特定标签的所有帖子。

只是寻找一些关于使用 Dynamo 处理此类邻接列表数据的建议,因为它看起来很有趣。谢谢!

最佳答案

邻接表的基本准则
您需要对建模方式进行一些修改。在邻接列表中,您有两种类型的项目:

  • 顶级(那些是您的 帖子 标签 )
  • 关联(表示 标签 与每个 帖子 相关联,反之亦然)

  • 要构建此邻接表,您必须遵循两个简单的准则(我认为您的示例中缺少这些准则):
  • 每个顶级项目(在您的情况下是 帖子 标签 )必须使用主键表示。此外,这些项目在排序键和主键中应该具有相同的值
  • 对于关联,使用主键表示源(或父),使用排序键表示目标(或子)。

  • 从我在你的例子中看到的,你设置了 的主键。帖子 标签 作为项目 ID,同时您也应该使用它的类型;例如 Post-1Tag-3 .在表示关联的项目中,我也没有看到您存储目标 ID。
    例子
    假设你有:
  • 三个帖子:“hello world”、“foo bar”和“Whatever...”
  • 以及三个标签:“酷”、“真棒”、“很棒”
  • 帖子“ Hello World ”有一个标签:“酷”
  • 帖子“foo bar”有两个标签:“cool”和“great”
  • 帖子“随便……”没有任何标签

  • 您需要在 Dynamo 中以这种方式建模:
    PRIMARY-KEY   | SORT-KEY    | SOURCE DATA  | TARGET DATA
    --------------|-------------|--------------|-------------
    Post-1 | Post-1 | hello world |
    Post-2 | Post-2 | foo bar |
    Post-3 | Post-3 | Whatever... |
    Tag-1 | Tag-1 | cool |
    Tag-2 | Tag-2 | awesome |
    Tag-3 | Tag-3 | great |
    Post-1 | Tag-1 | hello world | cool
    Post-2 | Tag-1 | foo bar | cool
    Post-2 | Tag-3 | foo bar | great
    Tag-1 | Post-1 | cool | hello world
    Tag-1 | Post-2 | cool | foo bar
    Tag-3 | Post-2 | great | foo bar
    你如何查询这个邻接表
    1)你需要一个特定的项目,比如 Post-1:
    查询 primary-key == "Post-1" & sort-key == "Post-1" - 退货:仅 Post-1
    2) 您需要与 Post-2 相关的所有标签:
    查询方式 primary-key == "Post-2" & sort-key BEGINS_WITH "Tag-" - 返回:Tag-1 和 Tag-3 关联。
    查询 the documentation关于 begin_with 关键条件表达式。
    3)你需要所有与相关的帖子,比如标签1:
    查询方式 primary_key == "Tag-1" & sort-key BEGINS_WITH "Post-" - 返回:Post-1 和 Post-2 关联。

    Note that, if you change the contents of a given post, you need to change the value in all association items as well.

    You can also don't store the post and tag content in association items, which saves storage space. But, in this case, you'd need two queries in the example queries 2 and 3 above: one to retrieve associations, another to retrieve each source item data. Since querying is more expensive than storing data, I prefer to duplicate storage. But it really depends if your application is read-intensive or write-intensive. If read-intensive, duplicating content in associations gives you benefit of reducing read queries. If write-intensive, not duplicating content saves write queries to update associations when the source item is updated.


    希望这可以帮助! ;)

    关于nosql - DynamoDB 邻接列表主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51051642/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com