DynamoDB M-M 邻接表设计模式 [英] DynamoDB M-M Adjacency List Design Pattern

查看:26
本文介绍了DynamoDB M-M 邻接表设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考

在桌子上,我不明白如何创建排序键?这是一个同时存储 Bill-ID 和 Invoice-ID 的属性吗?还是两个独立的属性?我有一种感觉,这是一个灵活的属性,如果是这样,你如何区分一个和另一个?我们打算如何在排序键上构建查询?

是否只是通过查看前缀Bill-"?还是发票-"?DynamoDB 的做法似乎是使用破折号(-")来分隔属性中的值.如果有人可以给我这些东西的用例,我也会很感激,但除非在这种情况下很重要,否则我会离开.

现在,这是非常相关且非常有趣的

先谢谢大家!!!:-)

解决方案

在您发布的基表(主键)中的项目的第一张图片中,如下所示:

First_id(Partition key) Second_id(Sort Key) 日期------------- ---------- ------发票-92551 发票-92551 2018-02-07发票-92551 Bill-4224663 2017-12-03发票-92551 Bill-4224687 2018-01-09发票-92552 发票-92552 2018-03-04发票-92552 Bill-4224687 2018-01-09Bill-4224663 Bill-4224663 2018-12-03Bill-4224687 Bill-4224687 2018-01-09

GSI 中相同的项目,这些项目看起来像这样

Second_id(Partition Key) First_id---------- ---------------发票-92551 发票-92551Bill-4224663 发票-92551Bill-4224687 发票-92551发票-92552 发票-92552Bill-4224687 发票-92552比尔-4224663 比尔-4224663比尔-4224687 比尔-4224687

他们以一种令人困惑的方式绘制了它.

  • 他们已将分区键合并到一个框中,但它们是单独的项目.
  • 他们还尝试在同一张图片中显示 GSI.您可以将基表和 GSI 视为两个保持同步的独立表,在很多方面都是如此.
  • 他们实际上并未为关键属性提供名称.在我的示例中,我将它们命名为 First_id 和 Second_id.

当您对基表进行查询时,您可以使用带有分区键 Invoice-92551 的查询,您将获得发票项目以及属于它的所有账单项目.

假设您正在应用程序中查看发票 Invoice-92551,您可以看到它有两个关联的账单(Bill-4224663Bill-4224687代码>).如果您点击账单,应用程序可能会对 GSI 进行查询.GSI 查询将具有分区键 Bill-4224687.如果您查看我上面绘制的 GSI 表,您会看到这将返回两个项目,表明 Bill-4224687 是两张发票的一部分(Invoice-92551Invoice-92552)

<小时>

在您的第二张图片中,bookID"和albumID"等字样应该代表实际 ID(比如 293847 和 3340876).

我会像这样画出他的例子:

ProductID(Partition Key) TypeID(Sort Key) Title Name------- ------ ------ ------专辑 1 专辑 1 黑暗面Album1 Album1:Track1 跟我说话专辑 1 专辑 1:Track2 BreatheMovie8 Movie8 白痴电影 8 电影 8:演员 1 卢克·威尔逊电影 8 电影 8:演员 2 玛雅·鲁道夫

以下是您的疑问:

分区键:Album1

为您提供专辑 1(黑暗面)的所有信息(包括曲目)

分区键:Album1 和排序键:Album1:Track2

仅提供有关呼吸的信息.

分区键:Movie8

为您提供关于 Movie8 (Idiocracy) 的所有信息(包括演员)

如果我正在构建表格,我会将其制作成电影、专辑等字样是实际 ID 的一部分(比如 Movie018274 和 Album983745987),但这不是必需的,它只是使 ID 更具可读性.

Referring to amazon. I was wondering if anyone could help me.

The first image is of the table, and the second is the GSI. Here is the table:

On the table, I don't understand how one is to create the sort-key? Is this one attribute that stores both Bill-ID and Invoice-ID? or two separate attributes? I have a feeling it's the one flexible attribute, and if so how do you differentiate one from the other? And how are we meant to construct the query on the sort-key?

Is it just by looking the prefix "Bill-" or "Invoice-"? The practice of DynamoDB seems to make use of dashes ("-") to separate values in an attribute. If anyone can give me use cases of such things, I would be grateful as well, but I am going off tangent unless it's important in this case.

Now, this is very relatable and very interesting YouTube, where the presenter uses ONE product table to store various types of items: Books, Song Albums, and Movies; and each has their own attributes.

Again I have a problem understanding the sort-key used there. I understand that productID=1 is bookID, and productID=2 is an Album. Now where it gets confusing now is what I circled in red. These are the tracks of Album 2. However, the structure of the sort key is "albumID:trackID". Now, where is the "trackID"? Is it meant to substitute the word "trackID" with actual ID? or is this meant to be a text exactly as "albumID:trackID"?.

What if I wanted to query a specific trackID? what would be the syntax of my query?

Please see the image here from the youtube:

Thank you all in advance!!! :-)

解决方案

In the first picture you posted the items in the base table (primary key) would look like this:

First_id(Partition key)        Second_id(Sort Key)          Dated
-------------                   ----------                  ------
Invoice-92551                   Invoice-92551               2018-02-07
Invoice-92551                   Bill-4224663                2017-12-03
Invoice-92551                   Bill-4224687                2018-01-09
Invoice-92552                   Invoice-92552               2018-03-04
Invoice-92552                   Bill-4224687                2018-01-09
Bill-4224663                    Bill-4224663                2018-12-03
Bill-4224687                    Bill-4224687                2018-01-09

And the same items in the GSI the items would look like this

Second_id(Partition Key)       First_id
----------                     ---------------
Invoice-92551                  Invoice-92551 
Bill-4224663                   Invoice-92551 
Bill-4224687                   Invoice-92551 
Invoice-92552                  Invoice-92552
Bill-4224687                   Invoice-92552
Bill-4224663                   Bill-4224663
Bill-4224687                   Bill-4224687 

They have drawn it in quite a confusing way.

  • They have merged the partition keys into one box, but they are separate items.
  • They have also tried to show the GSI in the same picture. You can think of the base table and the GSI as two separate tables that are kept in sync, in many ways that's what they are.
  • They haven't actually provided a name to the key attributes. In my example I have named them First_id and Second_id.

When you do a query on the base table, you can use a query with the partition key Invoice-92551 and you get both the Invoice item plus all the bill items that belong to it.

Imagine you are viewing invoice Invoice-92551 in an application and you can see it has two associated bills (Bill-4224663 and Bill-4224687). If you clicked on the bill, the application would probably do a query on the GSI. The GSI query would have partition key Bill-4224687. If you look at the GSI table I have drawn above, you can see this will return two items, showing that Bill-4224687 is part of two invoices (Invoice-92551 and Invoice-92552)


In your second picture, the words 'bookID' and 'albumID' etc are supposed to represent actual IDs (lets say 293847 and 3340876).

I would draw his example like this:

ProductID(Partition Key) TypeID(Sort Key)  Title          Name      
---------                ------            ------         ------
Album1                   Album1            Dark Side
Album1                   Album1:Track1     Speak to me
Album1                   Album1:Track2     Breathe
Movie8                   Movie8            Idiocracy
Movie8                   Movie8:Actor1                    Luke Wilson
Movie8                   Movie8:Actor2                    Maya Rudolph

Here are your queries:

Partition key: Album1

Gives you ALL the information (inc tracks) on Album 1 (Dark Side)

Partition key: Album1 and Sort Key: Album1:Track2

Gives you just the information on Breathe.

Partition key: Movie8

Gives you ALL the information (inc actors) on Movie8 (Idiocracy)

If I was building the table I would make it so the words Movie, Album etc were part of the actual ID (say Movie018274 and Album983745987) but that's not required, it just makes the IDs more human readable.

这篇关于DynamoDB M-M 邻接表设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆