DynamoDB
Boto3
hash key
range key
AWS Python SDK

Query DynamoDB with a hash key and a range key with Boto3

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

DynamoDB is a fully managed NoSQL database service provided by AWS, designed to deliver high performance and scalability for a range of applications. Unlike traditional SQL databases, DynamoDB uses primary keys to identify records. These keys can be composed of a partition key (known as a hash key) and an optional sort key (known as a range key). The combination allows for efficient query operations and data retrieval based on specific criteria.

In this article, we will explore how to query DynamoDB tables that employ both partition and sort keys using Boto3, AWS's SDK for Python. We will cover the basics, dive into technical examples, and provide insights into effective querying techniques.

Understanding Querying in DynamoDB

Before diving into querying with Boto3, it's essential to understand the concept of partition and sort keys:

  • Partition Key: This is a simple, unique key used to partition data across multiple servers. It determines the partition where your data is stored.
  • Sort Key: When a sort key is used in conjunction with a partition key, it allows for more complex querying capabilities, such as retrieving all records within a partition that match a specific range or condition.

Only items with the same partition key value are considered in the operations involving the sort key.

Setting Up Boto3

To interact with DynamoDB using Boto3, you first need to set up your Python environment and ensure you have the necessary credentials and configuration. Here's how to get started:

  1. Install Boto3:
bash
    pip install boto3
  1. Configure AWS Credentials: Ensure you have your AWS credentials configured. This involves setting up the ~/.aws/credentials file like so:
ini
1    [default]
2    aws_access_key_id = YOUR_ACCESS_KEY
3    aws_secret_access_key = YOUR_SECRET_KEY
4    region = YOUR_PREFERRED_REGION

Creating and Querying a Table

Creating a Table with Partition and Sort Key

First, ensure you have a table with a partition and a sort key. Use the following code to create a table if you don't have one already:

python
1import boto3
2
3dynamodb = boto3.resource('dynamodb')
4
5# Create a DynamoDB table
6def create_table():
7    table = dynamodb.create_table(
8        TableName='Music',
9        KeySchema=[
10            {
11                'AttributeName': 'Artist',
12                'KeyType': 'HASH'  # Partition key
13            },
14            {
15                'AttributeName': 'SongTitle',
16                'KeyType': 'RANGE'  # Sort key
17            }
18        ],
19        AttributeDefinitions=[
20            {
21                'AttributeName': 'Artist',
22                'AttributeType': 'S'
23            },
24            {
25                'AttributeName': 'SongTitle',
26                'AttributeType': 'S'
27            },
28        ],
29        ProvisionedThroughput={
30            'ReadCapacityUnits': 5,
31            'WriteCapacityUnits': 5
32        }
33    )
34    table.wait_until_exists()
35    return table
36
37table = create_table()

Querying with Boto3

Once your table is set up, querying involves specifying the partition key and, optionally, a condition for the sort key. Here's an example of how to run a query using Boto3:

python
1def query_table(artist, song_title_prefix):
2    table = dynamodb.Table('Music')
3
4    # Query the table
5    response = table.query(
6        KeyConditionExpression=boto3.dynamodb.conditions.Key('Artist').eq(artist) & 
7                              boto3.dynamodb.conditions.Key('SongTitle').begins_with(song_title_prefix)
8    )
9
10    return response['Items']
11    
12# Example usage
13items = query_table('Adele', 'Hello')
14for item in items:
15    print(item)

Explanation

  • KeyConditionExpression is a key component that specifies the condition for querying. In this case, we're querying all songs by a certain artist whose titles start with the given prefix.
  • boto3.dynamodb.conditions.Key is used to define the partition and sort key conditions.

Key Points

ComponentDescription
Partition KeyUniquely identifies the partition containing the data.
Sort KeyProvides more flexible querying within a partition.
KeyConditionExpressionUsed to specify query conditions.
Boto3AWS SDK for Python to interact with AWS services.
ProjectionExpressionLimit the attributes returned in query results.

Enhancing Queries

To further enhance your query operations, consider the following:

  • Filtering Results: Use FilterExpression to narrow down results post-query.
python
1    response = table.query(
2        KeyConditionExpression=Key('Artist').eq(artist),
3        FilterExpression=Attr('AlbumSales').gt(1000000)
4    )
  • Paginating Results: Use LastEvaluatedKey property for paginated query results.
  • Indexing: Consider creating Global Secondary Indexes (GSI) or Local Secondary Indexes (LSI) for additional querying flexibility without requiring changes to your primary key schema.

Conclusion

Querying DynamoDB with partition and sort keys through Boto3 provides a powerful mechanism to fetch data efficiently. By leveraging Boto3's capabilities and understanding DynamoDB's data models, developers can build robust, scalable applications. Always consider optimizing your queries with filtering, indexing, and paginations to ensure efficient use of resources and enhanced application performance.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.