0
0
AWScloud~5 mins

Partition key and sort key in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you store data in a database, you need a way to organize it so you can find it quickly. Partition key and sort key help split and order your data in AWS DynamoDB tables, making data retrieval fast and efficient.
When you want to store user profiles and quickly find a user by their unique ID.
When you have orders data and want to find all orders for a customer sorted by date.
When you need to organize sensor data by device and timestamp for fast queries.
When you want to avoid slow searches by using keys that directly point to data.
When you want to group related items together and sort them within that group.
Config File - dynamodb-table.json
dynamodb-table.json
{
  "TableName": "Orders",
  "AttributeDefinitions": [
    {"AttributeName": "CustomerId", "AttributeType": "S"},
    {"AttributeName": "OrderDate", "AttributeType": "S"}
  ],
  "KeySchema": [
    {"AttributeName": "CustomerId", "KeyType": "HASH"},
    {"AttributeName": "OrderDate", "KeyType": "RANGE"}
  ],
  "ProvisionedThroughput": {
    "ReadCapacityUnits": 5,
    "WriteCapacityUnits": 5
  }
}

TableName: The name of the DynamoDB table.

AttributeDefinitions: Defines the data types of the keys; 'S' means string.

KeySchema: Defines the partition key (HASH) and sort key (RANGE).

ProvisionedThroughput: Sets how much read and write capacity the table has.

Commands
This command creates a DynamoDB table named 'Orders' with CustomerId as the partition key and OrderDate as the sort key. It sets the read and write capacity to 5 units each.
Terminal
aws dynamodb create-table --cli-input-json file://dynamodb-table.json
Expected OutputExpected
{ "TableDescription": { "TableName": "Orders", "TableStatus": "CREATING", "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0, "KeySchema": [ { "AttributeName": "CustomerId", "KeyType": "HASH" }, { "AttributeName": "OrderDate", "KeyType": "RANGE" } ], "TableArn": "arn:aws:dynamodb:us-east-1:123456789012:table/Orders" } }
--cli-input-json - Specifies the JSON file with table configuration
This command checks the status and details of the 'Orders' table to confirm it was created successfully.
Terminal
aws dynamodb describe-table --table-name Orders
Expected OutputExpected
{ "Table": { "TableName": "Orders", "TableStatus": "ACTIVE", "KeySchema": [ { "AttributeName": "CustomerId", "KeyType": "HASH" }, { "AttributeName": "OrderDate", "KeyType": "RANGE" } ], "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0 } }
This command adds an order item to the 'Orders' table with CustomerId as the partition key and OrderDate as the sort key.
Terminal
aws dynamodb put-item --table-name Orders --item '{"CustomerId": {"S": "cust123"}, "OrderDate": {"S": "2024-06-01"}, "OrderAmount": {"N": "250"}}'
Expected OutputExpected
{}
This command retrieves all orders for the customer with ID 'cust123', using the partition key to find the group of items.
Terminal
aws dynamodb query --table-name Orders --key-condition-expression "CustomerId = :cid" --expression-attribute-values '{":cid": {"S": "cust123"}}'
Expected OutputExpected
{ "Items": [ { "CustomerId": {"S": "cust123"}, "OrderDate": {"S": "2024-06-01"}, "OrderAmount": {"N": "250"} } ], "Count": 1, "ScannedCount": 1 }
--key-condition-expression - Specifies the condition to find items by partition key
--expression-attribute-values - Defines the value for the key condition
Key Concept

If you remember nothing else from this pattern, remember: the partition key groups your data, and the sort key orders it within that group for fast and organized access.

Common Mistakes
Using only a partition key when you need to sort or filter items within a group.
Without a sort key, you cannot order or efficiently query items that share the same partition key.
Add a sort key to your table schema when you want to organize items within each partition.
Using a sort key as the partition key or mixing their roles.
The partition key determines data distribution and must be unique per group; the sort key orders items within that group.
Define the partition key as the main grouping attribute and the sort key as the secondary ordering attribute.
Querying without specifying the partition key in the key condition expression.
DynamoDB requires the partition key to be specified to efficiently find items; omitting it causes errors or full scans.
Always include the partition key in your query's key condition expression.
Summary
Create a DynamoDB table with a partition key and sort key to organize and order data.
Use the partition key to group related items and the sort key to sort them within the group.
Query the table by specifying the partition key to quickly find all related items.