0
0
DynamoDBquery~30 mins

Partition key distribution in DynamoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Partition Key Distribution in DynamoDB
📖 Scenario: You are building a simple DynamoDB table to store user orders for an online store. To keep the database fast and balanced, you need to choose a good partition key that spreads the data evenly.
🎯 Goal: Create a DynamoDB table with a partition key that distributes user orders evenly across partitions. You will define the table schema, set a partition key, and add sample items to see how the key affects data distribution.
📋 What You'll Learn
Create a DynamoDB table named Orders with a partition key called OrderID of type string.
Add a variable sample_orders containing 5 orders with unique OrderID values.
Write a query to retrieve all orders with a specific OrderID.
Add a secondary index on CustomerID to allow queries by customer.
💡 Why This Matters
🌍 Real World
Good partition key design helps DynamoDB distribute data evenly, improving speed and scalability for real online stores or apps.
💼 Career
Understanding partition keys and indexes is essential for database roles working with NoSQL databases like DynamoDB.
Progress0 / 4 steps
1
Create the DynamoDB table schema
Create a DynamoDB table named Orders with a partition key called OrderID of type string. Use the AWS SDK syntax to define the table schema in a variable called table_schema.
DynamoDB
Need a hint?

Define table_schema as a dictionary with keys TableName, KeySchema, AttributeDefinitions, and ProvisionedThroughput. The partition key is OrderID of type string.

2
Add sample orders data
Create a list called sample_orders with 5 dictionaries. Each dictionary should have keys OrderID (string), CustomerID (string), and Amount (number). Use these exact OrderID values: "O1001", "O1002", "O1003", "O1004", "O1005".
DynamoDB
Need a hint?

Make a list of 5 dictionaries with the exact OrderID values given. Each dictionary must also have CustomerID and Amount keys.

3
Write a query to get orders by OrderID
Write a function called get_order_by_id that takes a parameter order_id. Inside the function, write a query expression to get the order from sample_orders where OrderID matches order_id. Use a for loop with variables order to find the matching order and return it.
DynamoDB
Need a hint?

Define a function get_order_by_id that loops through sample_orders. Return the order where OrderID matches order_id. Return None if not found.

4
Add a secondary index on CustomerID
Update the table_schema dictionary to add a global secondary index named CustomerIndex. The index should have CustomerID as the partition key of type string. Add CustomerID to the AttributeDefinitions list as well.
DynamoDB
Need a hint?

Add a GlobalSecondaryIndexes key to table_schema. Define an index named CustomerIndex with CustomerID as the partition key. Also add CustomerID to AttributeDefinitions.