0
0
DynamoDBquery~30 mins

Querying GSI in DynamoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Querying a Global Secondary Index (GSI) in DynamoDB
📖 Scenario: You are managing a DynamoDB table that stores information about books in a library. Each book has a unique BookID as the primary key. You want to quickly find books by their Author using a Global Secondary Index (GSI).
🎯 Goal: Build a DynamoDB query to retrieve all books by a specific author using the GSI named AuthorIndex.
📋 What You'll Learn
Create a DynamoDB table named Books with BookID as the primary key.
Create a GSI named AuthorIndex with Author as the partition key.
Write a query to find all books by the author 'J.K. Rowling' using the GSI.
Use the AWS SDK for Python (boto3) syntax for the query.
💡 Why This Matters
🌍 Real World
Many applications need to query data by attributes other than the primary key. GSIs let you do this efficiently in DynamoDB.
💼 Career
Understanding how to create and query GSIs is essential for backend developers and database engineers working with DynamoDB.
Progress0 / 4 steps
1
Create the DynamoDB table data structure
Create a dictionary called table_definition that defines a DynamoDB table named Books with BookID as the primary key of type String. Include an attribute definition for BookID of type S.
DynamoDB
Need a hint?

Define table_definition as a dictionary with keys TableName, AttributeDefinitions, and KeySchema. Use BookID as the partition key.

2
Add the Global Secondary Index (GSI) configuration
Add a GlobalSecondaryIndexes key to the table_definition dictionary. Define one GSI named AuthorIndex with Author as the partition key of type S. Also add Author to the AttributeDefinitions list.
DynamoDB
Need a hint?

Update table_definition to include GlobalSecondaryIndexes with the correct index name and key schema. Also add Author to AttributeDefinitions.

3
Write the query to find books by author
Create a dictionary called query_params to query the Books table using the AuthorIndex. Set the IndexName to 'AuthorIndex', the KeyConditionExpression to check Author equals 'J.K. Rowling', and use the expression attribute values accordingly.
DynamoDB
Need a hint?

Use boto3.dynamodb.conditions.Key to build the KeyConditionExpression. Set IndexName to 'AuthorIndex' and TableName to 'Books'.

4
Complete the query call to DynamoDB
Write a line of code that calls dynamodb_client.query() with query_params and stores the result in a variable called response. Assume dynamodb_client is already created.
DynamoDB
Need a hint?

Use dynamodb_client.query(**query_params) to perform the query and assign the result to response.