DynamoDB Change Key Column Range

Is it possible to change the Rangekey column after creating the table. For example, adding a new column / attribute and assigning as RangeKey to the table. I tried the search but could not find articles about changing the range or hash

+13
amazon-dynamodb
source share
2 answers

No, unfortunately, it is not possible to change the hash key, range key, or indexes after creating the table in DynamoDB. The DynamoDB UpdateItem API documentation clearly states that indexes cannot be changed. I can’t find the link anywhere in the documents, which explicitly states that the keys of the table cannot be changed, but currently they cannot be changed.

Please note that DynamoDB is different from the scheme, different from the hash and range, and you can add other attributes to new elements without problems. Unfortunately, if you need to change either a hash key or a range key, you will need to create a new table and transfer the data.

Edit (January 2014) : DynamoDB now supports global secondary indexes on the fly

+20
source share

To change or create an additional sort key, you will need to create a new table and reconfigure it, since both steps cannot be performed for existing tables.

DynamoDB streams allow us to migrate tables without downtime. I did it very efficiently, and the following steps:

  1. Create a new table (let's call it NewTable) with the desired key structure, LSI, GSI.
  2. Enable DynamoDB Streams on source table
  3. Associate the lambda stream with the stream that places the entry in NewTable. (This lambda should clip the migration flag in step 5)
  4. [Optional] Create a GSI on the source table to speed up object scanning. Make sure that this GSI has only attributes: primary key and migrated (see Step 5).
  5. Scan the GSI created in the previous step (or the entire table) and use the following filter:

    FilterExpression = "attribute_not_exists (Migrated)"

Update each item in the table with the carry flag (ie, "Migrated": {"S": "0"}, which sends it to DynamoDB streams (using the UpdateItem API to ensure there is no data loss).

NOTE You might want to increase the recording capacity units in the table during updates.

  1. The lambda will collect all the items, clip the Migrated flag and paste it into NewTable.
  2. After transferring all elements, reassign the code to a new table.
  3. Delete the original table and the lambda function is once happy, all is well.

These steps should ensure that you do not have data loss or downtime.

I documented this on my blog using the code: https://www.abhayachauhan.com/2018/01/dynamodb-changing-table-schema/

+3
source share

All Articles