如何在DynamoDB中存储大型阵列

发布于 2025-02-08 17:02:20 字数 255 浏览 4 评论 0原文

我是DynamoDB的新手,我很好奇存储潜在大型阵列的最佳方法是什么。

我有一个看起来像这样的用户对象:

UserId: String
Watching: Card[]
Listings: Card[]

我知道发电机中对象的大小有限制 - 我认为1MB?因此,我认为如果用户有很多列表,它可能会超过此限制。 存储像这样的潜在大型阵列的最佳做法是什么? 可能是存储一系列Cardids,然后进行第二个查询以从中获得卡片?

I am new to DynamoDB and I am curious what is the best way to store potentially large arrays.

I have a user object that looks like this:

UserId: String
Watching: Card[]
Listings: Card[]

I am aware there is a limit to the size of objects in Dynamo - I think 1MB? Therefore I think if a user had many listings it might go past this limit.
What would be the best practice to store potential large arrays like this?
Would it be to maybe store an array of CardIds and then do a second query to get cards from this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

一直在等你来 2025-02-15 17:02:20

一种选项是使用单个表设计,列表在同一张表中。

用户项将具有用户#UID的主要键,而用户#UID(与PK相同)的SK,每个观看项目都有用户的PK #UID的SK>观看#wid,每个列表都有用户#uidlisting#lid 。

例如:

PKSK其他属性
用户#1用户#1
用户#2用户#2
用户#42用户#42是
用户#42观看#12可选
用户#42观看#29可选
用户#42列表列表#901可选
用户#42列表#472可选

方法对观看或列表关系的数量没有真正的限制。

然后,您只需通过发布pk =用户#42的查询来查询给定用户的所有项目,这将产生用户以及所有关联的观看和列表项目(尽管有分页)。您可以使用pk =用户#42sk begins_with(“ listing#”)查询给定用户的所有列表。

请注意,由于属性值的其他“用户”,“观看”和“清单”前缀,因此这将增加表大小,因此您可能需要考虑缩写这些。

引用亚历克斯·德布里(Alex Debrie):

在DynamoDB中使用单个表的主要原因是使用单个请求检索多个异源项目类型。

One option is to use the Single Table Design approach where all users, watchings, and listings are in the same table.

User items would have a primary key of user#uid and sk of user#uid (the same as pk), each watching item would have a pk of user#uid and sk of watching#wid, and each listing would have a pk of user#uid and sk of listing#lid.

For example:

pkskother attributes
user#1user#1yes
user#2user#2yes
user#42user#42yes
user#42watching#12optional
user#42watching#29optional
user#42listing#901optional
user#42listing#472optional

This approach has no real limit on the number of watching or listing relationships.

You can then query all items for a given user simply by issuing a query for pk=user#42 and that will yield the user and all associated watching and listing items (pagination notwithstanding). You can query all listings for a given user with pk=user#42 and sk begins_with("listing#").

Note that this will increase the table size because of the additional "user", "watching", and "listing" prefixes on attribute values so you may want to consider abbreviating those.

To quote Alex DeBrie:

The main reason for using a single table in DynamoDB is to retrieve multiple, heterogenous item types using a single request.

贩梦商人 2025-02-15 17:02:20

DynamoDB中对象的限制为400 kb,请参见 DynamoDB配额

对于较大的属性值,AWS建议在GZIP等格式中压缩属性,并将其存储在DynamoDB中。其他选项是将项目以JSON格式存储在S3中,并将该文件的密钥存储在DynamoDB中。

请参阅:用于存储大型物品和属性的最佳实践

大概是第三个选项是以某种方式将您的数组分开,并在DynamoDB中创建多个条目。或尝试为单独的属性创建单独的表,显然,如果例如listings数组的大小大于对象限制本身,则不会解决问题。

The limit of an object in DynamoDB is 400 KB, see DynamoDB Quotas.

For larger attribute values AWS suggests compressing of the attribute in formats such as GZIP, and store it in binary in DynamoDB. Other option would be to store the item in JSON format in S3 and store the key of this file in DynamoDB.

See: Best Practices for Storing Large Items and Attributes

Probably, a third option would be to split your array somehow, and create multiple entries in DynamoDB. Or try to create separate tables for separate attributes, obviously this wont solve the issue if for example Listings array's size is larger than the object limit itself.

贱人配狗天长地久 2025-02-15 17:02:20

DynamoDB中的表格,表中的每个行都是对象。

行中的项目以JSON对象格式查看。

该行无法支持JSON数组。

分成2个表。表A中的存储列表和表B中的另一个。设计主键并正确对键进行排序,以便可以识别记录属于特定用户。

The table in dynamoDB, every row in the table is object.

The item in a row view it in json object format.

The row cannot support json array.

Split into 2 table. Store listing in table A and another in table B. Design the primary key and sort key correctly so that it can identified the records belong to particular user.

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