Working With DynamoDB Columns/Attributes
Written by Andrea Perera
Published on December 24th, 2021
Time to 10x your DynamoDB productivity with Dynobase [learn more]
Amazon DynamoDB is a NoSQL database service that supports both key-value and document data structures. As a non-relational database, there is no such concept as a "column" in DynamoDB. Instead, DynamoDB consists of tables, items, and attributes.
DynamoDB Attributes - What You Need To Know
The main difference compared to a "column" based database is that each item in a DynamoDB table can have a different number of attributes.
In addition, DynamoDB supports several data types for attributes.
- Scalar Types (string, number, binary, Boolean, and null)
- Document Types (list and map)
- Set Types (number set, string set, and binary set)
These attributes could hold either a single value or an object having up to 32 levels of nested child attributes. This flexibility allows for a highly dynamic schema where each item can have a unique set of attributes.
Therefore, let's look at how to work with DynamoDB attributes in detail.
Working with DynamoDB Attributes
In order to add attributes, you need to first create a table. You can use AWS Console, AWS CLI, AWS SDK, or the DynamoDB Table Creation feature in Dynobase.
Suppose there is a Book table having the key attributes:
- Partition key - BookAuthor
- Sort key - BookTitle
You must first define the partition key and sort key pair since it's mandatory upon table creation. The below screenshot shows how to create the Book table using _Operation Builder_
in Dynobase.
DynamoDB Add Attribute To All Items
This is somewhat similar to adding a column in a SQL database. However, you don't have a direct way of adding an attribute to all the items in DynamoDB. Therefore, you need to scan the items to get their keys and update each item to add the attribute. Let's look at adding createdDate attribute to all the items.
Besides, if you are dealing with a large table, it's important to scan the table page-wise and add the attribute to each item. This can be achieved using pagination in the scan operation.
DynamoDB Add Attribute to Existing Item
Now let's look at adding attributes to individual items. First, you need to know the primary key and the sort key of the item. Then you can run an update expression to add the attribute. I'm going to add the attribute isSold only to a selected item.
DynamoDB Rename Attributes
You may think this is similar to renaming a column in a SQL database. However, in DynamoDB, you cannot directly rename an existing attribute. First, you need to add a new attribute for each of the entries and remove all the values for the existing attribute.
Let's look at how we can rename isSold to userPurchased.
DynamoDB Remove Attributes from an Existing Item
You can use the REMOVE clause in an update expression to delete an attribute from an item. Nothing happens even if trying to delete an attribute that does not exist.
Let's look at removing the userPurchased attribute from an item.
DynamoDB Remove Attribute From All Items
To give a SQL analogy, this is like removing a column from DynamoDB. You can do this by running a scan and looping through the update expression for each item. Let's remove userPurchased attribute from all the items.
DynamoDB Query Attribute
When retrieving items from DynamoDB, you can use the query operation. If the table contains only the partition key, you can use it to query items in the table. Otherwise, you need to know the partition key and the values to compare against the sort key.
You can use the filter option in Dynobase for fast filtration at development time.
DynamoDB Query Two Attributes
The below code shows how to query attributes in DynamoDB using Java. The Book table consists of BookAuthor (partition key) and BookTitle (sort key) attributes.
If you need to query items based on non-key attributes, you can still perform it using the scan operation. However, it is expensive and slow compared to the query operation as it has to compare against all the items in a table.
DynamoDB Query Nested Attributes
As you have seen, in DynamoDB, you can query based on the primary key, sort key, or use the keys in LSI or GSI. Since key condition expressions for the query are limited, you cannot query by nested attributes.
DynamoDB Scan Return All Attributes
DynamoDB scan method will scan through the entire table and return all of the table's data attributes for each item. A single Scan request can only retrieve 1 MB of data at a time.
The below code shows how to scan the table and return all the attributes for each item in DynamoDB using Java. The name of the table to scan is specified in the ScanRequest instance.
DynamoDB List Attributes
In DynamoDB, there is no direct way to get the list of attributes in a table. The only possible way is to query the items and list down the attributes.
FAQ
Does DynamoDB have attributes/columns?
There is no such thing as a "column" in DynamoDB as it's not a relational database. DynamoDB is a key-value NoSQL database with items and attributes.
How many attributes/columns can DynamoDB have?
As there are no columns in DynamoDB, you can add a new item with a new attribute. An item can have one or more attributes. The maximum size of an item, including all attribute names and values, must be 400KB.
What are the types of attributes/columns DynamoDB can have?
Within a table, DynamoDB allows a variety of data types for attributes such as Scalar Types (string, number, binary, Boolean, and null), Document Types (list and map), and Set Types (number set, string set, and binary set).
Is DynamoDB column-based?
No, it's not column-based. DynamoDB is a NoSQL database that supports both key-value and document data structures.