0
0
DynamoDBquery~30 mins

Composite sort key pattern in DynamoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Composite Sort Key Pattern in DynamoDB
📖 Scenario: You are building a simple DynamoDB table to store information about books in a library. Each book has a unique BookID and belongs to a category. You want to organize the data so that you can query all books in a category sorted by their publication year and title.
🎯 Goal: Create a DynamoDB table with a composite sort key pattern that combines PublicationYear and Title to allow sorting books within each category by year and then by title.
📋 What You'll Learn
Create a DynamoDB table named Library with Category as the partition key and SortKey as the sort key.
The SortKey should combine PublicationYear and Title separated by a hash (#).
Insert three books with exact data provided.
Write a query to get all books in the Science category sorted by the composite sort key.
💡 Why This Matters
🌍 Real World
Composite sort keys are used in DynamoDB to organize related data items so they can be efficiently queried and sorted within a partition, such as sorting books by year and title within a category.
💼 Career
Understanding composite sort keys is essential for designing scalable and efficient NoSQL databases, a key skill for backend developers and database engineers working with DynamoDB.
Progress0 / 4 steps
1
Create the DynamoDB table and insert books
Create a DynamoDB table named Library with Category as the partition key and SortKey as the sort key. Insert these three books as items with attributes: Category, SortKey, BookID, Author. Use the exact values: {Category: 'Science', SortKey: '2020#Physics Fundamentals', BookID: 'B001', Author: 'John Doe'}, {Category: 'Science', SortKey: '2018#Chemistry Basics', BookID: 'B002', Author: 'Jane Smith'}, {Category: 'Literature', SortKey: '2019#Modern Poetry', BookID: 'B003', Author: 'Emily Clark'}.
DynamoDB
Need a hint?

Remember to define Category as the partition key and SortKey as the sort key. Combine PublicationYear and Title with a hash (#) for the sort key.

2
Define a helper function to create the composite sort key
Create a helper function called create_sort_key that takes publication_year and title as parameters and returns a string combining them with a hash (#) in between, like "2020#Physics Fundamentals".
DynamoDB
Need a hint?

Use an f-string to combine publication_year and title with a hash (#) in between.

3
Use the helper function to insert a new book item
Use the create_sort_key function to create the SortKey for a new book with Category 'Science', PublicationYear 2021, Title 'Astronomy Basics', BookID 'B004', and Author 'Alice Johnson'. Insert this item into the Library table.
DynamoDB
Need a hint?

Use the helper function to create the sort key string, then add the new item to the Library dictionary with the correct keys and values.

4
Query books in the Science category sorted by composite sort key
Write a query to get all books in the Science category sorted by the SortKey. Use a for loop to iterate over the Library items, filter by Category equal to 'Science', and collect the items sorted by SortKey in ascending order.
DynamoDB
Need a hint?

Filter the items by Category and then sort them by SortKey using sorted() with a lambda function.