简体   繁体   English

如何使用 dynamodb 表设计来证明这些可能的需求变化(交换主键列)?

[英]How to future proof these possible requirement changes (swaping primary key columns) with a dynamodb table design?

I have the following data structure我有以下数据结构

item_id String
version String
_id     String 
data    String

_id is simply a UUID to identify the item. _id只是一个用于标识项目的 UUID。 There is no need to search for a row by this field yet.目前还不需要通过该字段搜索行。

As of now, item_id, an id generated by an external system, is the a primary key.截至目前,由外部系统生成的 id item_id 是主键。 ie Given the item_id, I want to be able retrieve version , _id and data from the dynamodb table.即给定 item_id,我希望能够从 dynamodb 表中检索version_iddata

item_id -> (version, _id, data)

Therefore I am setting item_id as the partition key.因此我将item_id设置为分区键。

I have two questions for future-proofing (evolution of) the above "schema":对于上述“模式”的未来验证(演变),我有两个问题:

  1. In the future, if I want to incorporate version (version number of the item) into the primary key, can I just modify the table and add it to be the partition key?以后如果想把version(item的版本号)合并到primary key中,是不是直接修改table,把它加成partition key就可以了?

  2. If I also want to make the data searchable by _id , is it feasible modify the table to assign _id to be the partition key (It is a unique value because it is a UUID) and reassign item_id to be a search key?如果我还想通过_id搜索数据,是否可以修改表以将_id分配为分区键(这是一个唯一值,因为它是 UUID)并重新分配item_id为搜索键?

I want to avoid creation of new dynamodb table and data migration to create new key structures, because it may lead to down time.我想避免创建新的 dynamodb 表和数据迁移以创建新的密钥结构,因为这可能会导致停机。

You cannot update primary keys in DynamoDB.您无法更新 DynamoDB 中的主键。 From the docs :文档

You cannot use UpdateItem to update any primary key attributes.您不能使用 UpdateItem 更新任何主键属性。 Instead, you will need to delete the item, and then use PutItem to create a new item with new attributes.相反,您需要删除该项目,然后使用 PutItem 创建具有新属性的新项目。

If you wanted to make data searchable by _id , you could introduce a secondary index with the _id field as the partition key of the index.如果你想让数据可以通过_id搜索,你可以引入一个二级索引,以_id字段作为索引的分区键。

For example, let's say your data looked like this:例如,假设您的数据如下所示:

在此处输入图像描述

If you defined a secondary index on _id , the index would look like this (same data as the previous example, just a different logical view):如果您在_id上定义了二级索引,则该索引将如下所示(与上一个示例相同的数据,只是不同的逻辑视图):

在此处输入图像描述

DynamoDB doesn't currently have any native versioning functionality, so you'll have to incorporate that into your data model. Fortunately, there's lots of discussion about this use case on the web. AWS has a document of DynamoDB "Best Practices", including an example of versioning . DynamoDB 目前没有任何本机版本控制功能,因此您必须将其合并到您的数据中 model。幸运的是,在 web 上有很多关于此用例的讨论。AWS 有 DynamoDB“最佳实践”文档,包括版本控制的一个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM