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

查看:312
本文介绍了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请求,条件是主键为null,如 API参考。这将导致第二次注释保存失败,您可以向用户报告错误,或者只是使用更新的时间戳重试。没有条件,第二条注释将覆盖第一条注释。另外,您可以使表的范围键成为与userId串联的datePosted的复合值。这样,范围键将始终是唯一的,但仍将按日期时间顺序进行排序。这是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天全站免登陆