跨主索引和全局二级索引的 DynamoDB 键唯一性 [英] DynamoDB key uniqueness across primary and global secondary index

查看:18
本文介绍了跨主索引和全局二级索引的 DynamoDB 键唯一性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 DynamoDB 表来保存与单个对象关联的评论.

I'm working on creating a DynamoDB table to hold comments associated to a single object.

评论在特定时间发布到对象,我使用发布的时间作为范围,因此评论可以按时间降序排序.我有一个发布评论的用户的 userId 的全局二级索引,这应该允许我获取给定用户发布的所有评论.

Comments are posted to an object at a specific time, I am using the time posted as the range so comments are sortable in time descending order. I have a global secondary index of the userId of the user who posted the comment, this should allow me to get all comments posted by a given user.

我的问题是,这把钥匙是唯一的吗?我担心,由于两个用户在技术上可以同时向同一个 objectId 发表评论,因此评论哈希和范围键将是相同的.

My question is, will this key be unique? I'm worried that since it is technically possible for two users to post a comment to the same objectId at the same time, the comment hash and range key will be identical.

我的希望是,由于同一个用户不可能同时对同一个对象发表两条评论,因此全局二级索引将使键唯一.

My hope is that since it should be impossible for the same user to post two comments on the same object at the same time the global secondary index will make the key unique.

 Comment table:

   Hash Key                   Range Key                      Global Secondary Index Hash
 ---------------------------------------------------------------------------------------
|   objectId   |              datePosted                 |           userId             |
| (not unique) |      (not unique if multiple users      | (unique across objectId and  |
|              |  post for the same object @ same time)  |         datePosted)          |
 ---------------------------------------------------------------------------------------

推荐答案

DynamoDB 索引与唯一性无关.全球本地索引允许有重复的哈希键 &范围密钥对.只有表本身的哈希键和范围键是唯一的.

DynamoDB indexes have nothing to do with uniqueness. Global and local indexes are allowed to have duplicate hash key & range key pairs. Only the hash key and range key of the table itself are unique.

在您的示例中,两个不同的用户可能在同一时刻对一个对象发表评论,并产生一个重复的 objectId、datePosted 键.有几种方法可以解决这个问题.您可以使用 PutItem 请求,条件是主键为空,如 API 参考.这将导致第二次评论保存失败,您可以向用户报告错误或简单地使用更新的时间戳重试.没有条件,第二条评论将覆盖第一条.或者,您可以将表的范围键设置为 datePosted 与 userId 连接的复合值.这样,范围键将始终是唯一的,但仍将按日期时间顺序排序.这是 DynamoDB 的常见做法.

In your example, it would be possible for two different users to comment on an object at the same exact moment and produce a duplicate objectId, datePosted key. There are a couple ways to deal with this. You could use a PutItem request with a condition that the primary key is null as mentioned in the API reference. That would cause the second comment save to fail and you could report an error to the user or simply try again with an updated time-stamp. Without the condition, the second comment will overwrite the first. Alternatively, you could make the range key of the table a composite value of datePosted concatenated with userId. That way, the range keys will always be unique, but will still be sorted in date time order. This is a common practice with DynamoDB.

这篇关于跨主索引和全局二级索引的 DynamoDB 键唯一性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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