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 discuss 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 single primary key

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

Primary Key in DynamoDB

Step 2: Insert some data in the ClodAffaire table

aws dynamodb batch-write-item ^
--request-items file://employee.json ^
--endpoint-url http://localhost:8000

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

aws dynamodb scan ^
--table-name CloudAffaire ^
--attributes-to-get id ^
--endpoint-url http://localhost:8000 ^
--output text

Primary Key in DynamoDB

Observe: Data are not sorted in any order. Because we have created a simple primary key on id, DynamoDB have 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

aws dynamodb delete-table ^
 --table-name CloudAffaire ^
 --endpoint-url http://localhost:8000

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

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

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

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

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 country is not part of primary key. To query using non primary key, we need to create secondary index on the table which will be covered in the next blog post.

Delete the CloudAffaire table:

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

 

Leave a Reply

Close Menu