| by Arround The Web | No comments

DynamoDB Composite Keys: Everything You Should Know

AWS DynamoDB relies on tables and indexes to store and organize the data. In these tables, there are primary keys that help in data organization and item retrieval. Thus, a DynamoDB table or index always have a simple primary key or composite primary key.

We already discussed the simple primary keys on this website. This article focuses on DynamoDB partition keys. The sections that we cover include the definition of a composite key, the best practices for creating composite keys, and how to create a composite key for your table.

What Is a DynamoDB Composite Key?

A DynamoDB composite key is a set of attributes consist of a partition key and a sort key. All tables with sort keys have composite primary keys, while all tables without sort keys have simple primary keys. They are also known as hash-range keys or DynamoDB composite primary keys.

Interestingly, composite keys do not exist within the items in the table as with the other attributes. Instead, it is an abstract concept that you can craft or think of as a combination of a partition key and a sort key.

Consider the composite keys as your primary keys when querying your tables or when undertaking any operations that require a primary key. Thus, all Update, Delete, Update, or any items that need the inclusion of a primary key have both the partition key and the sort key.

While a partition key or a simple primary key still helps in most operations, the composite keys add an efficiency in most DynamoDB operations and improve the performance of your database. Besides, partition keys also enable you to filter the query results more efficiently using the comparison operations.

Best Practices to Create the DynamoDB Composite Keys

Ensure that you adhere to the following:

Pick a Unique Partition Key: Your partition key should be unique and have as high cardinality as possible. A partition key with an extremely low cardinality may affect your data storage and distribution.

You can use the values that are not shareable among the items in a table. They can include the email addresses, identification numbers, or user names. Therefore, a USER_ID works well in the partition key column for a USERS table. Likewise, an EMPLOYEE table can have the EMPLOYEE_ID as the partition key.

Your Sort Key Should Complement the Access Patterns: Your sort key should gather the related information in a single place for efficient query operations. A good sort key improves the selectivity of your table’s items during retrieval by a query command.

How to Create a Composite Key in DynamoDB

To create a composite key, you can use the create-table command with the –key-schema and –attribute-definitions parameters.

The –key-schema parameter specifies the primary key structure of the table and can include one or more elements. Specify the key schema element first, followed by any secondary indexes. Each element in the key schema consists of an attribute name and a key type.

On the other hand, the –attribute-definitions parameter specifies the attributes that make up the primary key and any secondary indexes. Each attribute definition consists of an attribute name and an attribute type.

Here’s an example on how you can use these parameters to create a DynamoDB table with a composite primary key consisting of two attributes: partition_key and sort_key:

aws dynamodb create-table \
  --table-name <my-table-name> \
  --attribute-definitions AttributeName=partition_key,AttributeType=S AttributeName=sort_key,AttributeType=S \
  --key-schema AttributeName=partition_key,KeyType=HASH AttributeName=sort_key,KeyType=RANGE \
  --provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=10

 
The given command creates a new DynamoDB table with a composite primary key consisting of the partition_key attribute as the partition (HASH) key, and the sort_key attribute as the sort (RANGE) key. Proceed to define the primary key attributes in the –attribute-definitions parameter and the key schema in the –key-schema parameter.

You can also create a composite key by simply defining both the item’s partition key and composite key using the PutItem operation. For example:

DynamoDBClient.putItem({
    "TableName": "myTableName",
    "Item": {
        "pk": {
            "S": "PartitionKey"
        },
        "sk": {
            "S": "SortKey"
        },
        "email": {
            "S": "abcd@test.com"
        }
    }
})

 

Conclusion

This article contains everything you need to know about DynamoDB composite keys. Notably, it’s essential to choose the partition key and sort key carefully, as they significantly impact the performance and scalability of your DynamoDB table. A composite primary key is ideal for querying and filtering the data in DynamoDB based on the partition key and sort key.

Share Button

Source: linuxhint.com

Leave a Reply