How to Add a Column in DynamoDB

Amazon Web-ServicesNosqlAmazon Dynamodb

Amazon Web-Services Problem Overview


Is there a way to add a new column to existing table in DynamoDB in Amazon's AWS?

Google didn't help,

UpdateTable Query in http://docs.aws.amazon.com/cli/latest/reference/dynamodb/update-table.html?highlight=update%20table doesn't have any information related to adding a new column.

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

DynamoDB does not require schema definition, and so there is no such thing as a "column". You can just add a new item with a new attribute.

Solution 2 - Amazon Web-Services

Well, let's not get dragged away in the semantic discussion about the difference between "fields" and "columns". The word "column" does remind us of relational databases, which dynamodb is not. In essence that means that dynamodb does not have foreign keys.

Dynamodb does have "primary partition keys" and "index partition keys" though, just as with relational databases.

You do need to respect those keys when you add data. But aside from those requirements, you don't have to predefine your fields (except for those partition keys mentioned earlier).

Assuming that you are new to this, some additional good practices:

  • Add a numeric field to each record, to store the time of creation in seconds. Dynamodb has optional cleaning features, which require this type of field in your data.
  • You cannot use dates in dynamodb, so you have to store those as numeric fields or as strings. Given the previously mentioned remark, you may to prefer a numeric type for them.
  • Don't store big documents in it, because there is a maximum fetch size of 16MB, and a maximum record size of 400KB. Fortunately, AWS has a DocumentDB as well.

Solution 3 - Amazon Web-Services

I installed NoSQL Workbench then connected to existing DynamoDB Table and tried to update existing Item by adding a new attribute. I figured out that we can only add a new attribute with one of these types - "SS", "NS", "BS" (String Set, Number Set, Binary Set"). In Workbench, we can generate code for the chosen operation.

enter image description here

I scanned my dynamodb Table and for each item added new attribute with type "SS" then I scanned again and updated recently added new attribute to type - "S" in order create a global secondary index (GSI) with a primary key - "pk2NewAttr".

NoSQL Workbench related video - https://www.youtube.com/watch?v=Xn12QSNa4RE&feature=youtu.be&t=3666

Example in Python "how to scan all dynamodb Table" - https://gist.github.com/pgolding

Solution 4 - Amazon Web-Services

A way to add a new column to existing table in DynamoDB in Amazon's AWS:

We can store the values in DynamoDb in 2 ways, (i) In an RDBMS Type of Structure for the DynamoDB, we can add a new Coulmn by executing the same command keeping the "new Column" entry within which the Records in the Existing Table has been created. we can use DynamoDb with the Records/ Rows having Values for certain Columns while other columns does not have Values.

(ii) In a NoSQL kind of Structure; where we store a Json String within a Column to keep all the attributes as per the Requirement. Here we are generating a json string and we have to add the new Attribute into the json String which can then be inserted into the same Column but with the new Attribute.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArun Selva KumarView Question on Stackoverflow
Solution 1 - Amazon Web-ServicesJulio FaermanView Answer on Stackoverflow
Solution 2 - Amazon Web-ServicesbvdbView Answer on Stackoverflow
Solution 3 - Amazon Web-ServicesSAndriyView Answer on Stackoverflow
Solution 4 - Amazon Web-Servicesuser11677199View Answer on Stackoverflow