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
1 2 3 4 5 6 7 |
aws dynamodb create-table ^ --table-name CloudAffaire ^ --attribute-definitions AttributeName=company,AttributeType=N ^ --key-schema AttributeName=id,KeyType=HASH ^ --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 ^ --endpoint-url http://localhost:8000 ^ --output table |
Step 2: Insert some data in the ClodAffaire table
1 2 3 |
aws dynamodb batch-write-item ^ --request-items file://employee.json ^ --endpoint-url http://localhost:8000 |
Note: You can download the employee.json file from github
https://github.com/CloudAffaire/DynamoDB
Step 3: Scan the table data
1 2 3 4 5 |
aws dynamodb scan ^ --table-name CloudAffaire ^ --attributes-to-get id ^ --endpoint-url http://localhost:8000 ^ --output text |
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
1 2 3 |
Step 4: Create a composite primary key (Partition Key = company, Sort Key = id)
1 2 3 4 5 6 7 |
aws dynamodb create-table ^ --table-name CloudAffaire ^ --attribute-definitions AttributeName=company,AttributeType=S AttributeName=id,AttributeType=N ^ --key-schema AttributeName=company,KeyType=HASH AttributeName=id,KeyType=RANGE ^ --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 ^ --endpoint-url http://localhost:8000 ^ --output table |
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
1 2 3 4 5 6 7 |
aws dynamodb query ^ --table-name CloudAffaire ^ --projection-expression "id" ^ --key-condition-expression "company = :comp" ^ --expression-attribute-values "{"":comp"": {""S"": ""Amazon""}}" ^ --endpoint-url http://localhost:8000 ^ --output text |
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.
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:
1 2 |
aws dynamodb delete-table --table-name CloudAffaire ^ --endpoint-url http://localhost:8000 |
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