Primary Key In DynamoDB

Primary Key In DynamoDB

Primary Key in DynamoDB

Hello Everyone

Welcome to CloudAffaire and this is Debjeet

In the last two blog posts, we have discussed some of the DynamoDB API’s available.

https://cloudaffaire.com/dynamodb-api-part-one/

https://cloudaffaire.com/dynamodb-api-part-two/

In this blog post, we are going to discuss Primary Key in DynamoDB.

Primary Key in DynamoDB:

When you create a table, in addition to the table name, you must specify the primary key of the table. The primary key uniquely identifies each item in the table, so that no two items can have the same key.

DynamoDB supports two different kinds of primary keys:

  • Partition key (Single Primary Key)
  • Partition key and sort key (Composite Primary Key)

Partition key:

A simple primary key, composed of one attribute known as the partition key. DynamoDB uses the partition key’s value as input to an internal hash function. The output from the hash function determines the partition (physical storage internal to DynamoDB) in which the item will be stored. In a table that has only a partition key, no two items can have the same partition key value.

Partition key and sort key:

Referred to as a composite primary key, this type of key is composed of two attributes. The first attribute is the partition key, and the second attribute is the sort key. DynamoDB uses the partition key value as input to an internal hash function. The output from the hash function determines the partition (physical storage internal to DynamoDB) in which the item will be stored. All items with the same partition key value are stored together, in sorted order by sort key value.

Next, we are going to create both single primary key and composite primary key in our local DynamoDB instance.

Step 1: Create a table with a single primary key

Primary Key in DynamoDB

Step 2: Insert some data in the ClodAffaire table

Primary Key in DynamoDB

Note: You can download the employee.json file from github

https://github.com/CloudAffaire/DynamoDB

Step 3: Scan the table data

Primary Key in DynamoDB

Observe: Data is not sorted in any order. Because we have created a simple primary key on id, DynamoDB has not sorted the data in any order. In order to sort the data, we have to create a composite primary key and define the sort key.

Delete the CloudAffaire table

Step 4: Create a composite primary key (Partition Key = company, Sort Key = id)

Primary Key in DynamoDB

Note: Partition key is defined as keytype=HASH and sort key is defined as keytype=RANGE. We have sorted the data on id.

Step 5: Query the table data where company=Amazon

Primary Key in DynamoDB

Observe: The data is sorted in id.

Next, let’s try to query on a different element (country) which is not part of the primary key.

Primary Key in DynamoDB

Note: We are unable to query the table as the country is not part of the primary key. To query using a non-primary key, we need to create a secondary index on the table which will be covered in the next blog post.

Delete the CloudAffaire table:

Hope you have enjoyed this article. In the next blog post, we will discuss Secondary Index in DynamoDB

To get more details on DynamoDB, please refer below AWS documentation

https://docs.aws.amazon.com/dynamodb/index.html

 

Leave a Reply

Close Menu