如何在DynamoDB中实现一对多关系

huangapple go评论68阅读模式
英文:

How to implement one-to-many relationships with DynamoDB

问题

大家好!我有两个表格,我想通过DynamoDb将它们连接起来,但由于后者不是关系型数据库,我不知道如何映射这两个表格之间的链接。
特别地,我有一个价格列表表格和一个包含了第一个表格细节的详细列表表格。我如何在使用Spring Boot的情况下,使用DynamoDB实现Java中的一对多关系呢?

英文:

everyone! I have two tables that I would like to join via DynamoDb, but since the latter is not a relational db, I don't know how to map the link between the two tables.
In particular, I have a Price List table and a Detail List table that contains the details of the first one. How can I implement one-to-many relationship in java using dynamoDB with Spring Boot?

答案1

得分: 1

DynamoDB基本上是一个键值存储。您只会根据键执行查找。该键可以是人为的,不仅仅是用户ID,也可以是类似于"user_id#product#order"的内容,但仍然是基于键的查找。如果要使用DynamoDB,必须以使所有查询都归结为基本键值访问(加上一些排序)的方式存储数据。您必须完全相反地对数据进行规范化,并将关系拆分成多个表:您必须对所有数据进行反规范化,将数据和所有关系存储在同一个表中,多次存储,使用多个复杂的人工键。有关如何使用LSI、GSI、如何建模数据、如何选择人工键等信息,请参阅例如https://www.youtube.com/watch?v=HaEPXoXVf2k。

这意味着您将没有ItemOrderOrderItem表需要连接在一起,而是只会有一个名为Everything的表,其中可能包含字段:userid、username、ordernumber、itemid、itemprice、itemquantity、itemname、orderdate、shippingaddress等。
如果一个订单中有三个商品,那么在这个表中将会有三个条目。这意味着用户名会经常出现在表中,商品名也会经常出现在表中,而且更改它们将会很困难,但如果要使用DynamoDB,事情就是这样。

这就是您如何对一对多关系建模,将它们打包到一个单独的表中并添加适当的索引。

如果您对数据的当前或未来访问模式或如何正确结构化数据毫无头绪,那么DynamoDB对您来说可能不是合适的工具。

英文:

DynamoDB is basically a key-value store. You only every perform a lookup based on a key. That key may be artificial, not just a user id, but maybe "user_id#product#order" but still it will be a key-based lookup. If you want to use DynamoDB you have to store the data in a way that all queries that you will need will all boil down to basic key-based access (plus some sorting).
You have to do the exact opposite of normalizing your data and splitting relations into multiple tables: you have to de-normalize all your data to store the data and all the relations just in one table, multiple times, with multiple complex artificial keys. See e.g. https://www.youtube.com/watch?v=HaEPXoXVf2k on how to use LSIs, GSIs, how to model your data, how to choose artificial keys, etc.

That means you will not have Item, Order and OrderItem table that you join together, but you will have just one Everything table which may have the fields: userid, username, ordernumber, itemid, itemprice, itemquantity, itemname, orderdate, shippingaddress, etc.
And if you have three items in an order you will have three entries in this table. That means the username will be in the table very often, that means the itemname will be in the table very often and changing them will be difficult but that is how things are if you want to use dynamodb.

That is how you model one-to-many relations, by packing them into a single table and add proper indexes.

If you do have no idea about the current or future access patterns of your data or how to structure your data properly then dynamodb is the wrong tool for you.

答案2

得分: 0

你所提出的问题触及了与 DynamoDB 和 NoSQL 数据建模密切相关的本质。将你对关系型数据库的了解简单应用于 DynamoDB 并不是一件简单的事情。在解决这个问题之前,请花点时间熟悉一下 DynamoDB 的基础知识。

观看关于在 DynamoDB 中建模一对多关系的这个视频。我建议你从开头开始观看整个视频,因为它是目前为止关于这个主题的最好的入门视频。

英文:

The question you are asking gets at the very essence of working with DynamoDB and NoSQL data modeling. It is not as simple as applying your relational database knowledge to DynamoDB. Take a moment to familiarize yourself with the DynamoDB basics before you get too far into solving this problem.

Watch this video about modeling one-to-many relationships in DynamoDB. I would recommend you watch the entire video from the beginning, as it's one of the best introductions to the topic currently available.

huangapple
  • 本文由 发表于 2020年9月25日 21:02:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64064721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定