AWS DynamoDB with Python (Boto3) — Part 3 — Query Items from DynamoDB

Vicky AV
3 min readSep 21, 2022
Accessing DynamoDB with Python

If you haven’t read the previous article in this series AWS DynamoDB with Python (Boto3) — Part 2 — Create Table & Insert Item

Query Item from DynamoDB

To select items from DynamoDB which has both Partition Key & Sort Key, we need to mandatorily supply at least the Partition Key. Sort Key is optional.

We can also apply filters like begins with, contains on Sort Key & any other attribute in the item other than the partition key

Consider we want to query a student belonging to a particular department. Here is how we can achieve it (read the previous articles in this series to understand the data model)

import boto3
from boto3.dynamodb.conditions import Key, Attr
class Student:
dept: str
stud_id: str
stud_name: str
stud_age: int
subjects: list

def __init__(self, dept, stud_id, stud_name, stud_age, subjects):
self.dept = dept
self.stud_id = stud_id
self.stud_name = stud_name
self.stud_age = stud_age
self.subjects = subjects
def select_a_student(dept, stud_id):
dynamodb = boto3.resource('dynamodb',
aws_access_key_id="dummy",
aws_secret_access_key="dummy",
region_name="local",
endpoint_url="http://localhost:8000")

table = dynamodb.Table('Student')

response = table.query(
KeyConditionExpression=Key('dept').eq(dept) & Key('stud_id').eq(stud_id)
)

print(response['Items'])

boto3 provides classes like Key, Attr etc. to represent in Python

Key class has a function called eq meaning equals. Also note how multiple Key conditions are separated using & (AND) operator

Consider another case where we want to query all students belonging to a particular department. Here is how we can achieve it

def select_all_students_from_dept(dept):
dynamodb = boto3.resource('dynamodb',
aws_access_key_id="dummy",
aws_secret_access_key="dummy",
region_name="local",
endpoint_url="http://localhost:8000")

table = dynamodb.Table('Student')

response = table.query(
KeyConditionExpression=Key('dept').eq(dept)
)

print(response['Items'])

To query all data from a particular partition, we can simply specify the Partition Key along in KeyConditionExpression

Applying filters on Non-Key Attributes

Consider we want to query all students belonging to a particular department whose name begins with some String. Here is how we can achieve it

def filter_students_with_name_begins_with(dept, name):
dynamodb = boto3.resource('dynamodb',
aws_access_key_id="dummy",
aws_secret_access_key="dummy",
region_name="local",
endpoint_url="http://localhost:8000")

table = dynamodb.Table('Student')

response = table.query(
KeyConditionExpression=Key('dept').eq(dept),
FilterExpression=Attr('stud_name').begins_with(name)
)

print(response['Items'])

Attr class offers function called begins_with using which we filtered all student whose stud_name started with given input

Since DynamoDB attribute can also be of type list, is it possible to check if list contains particular value ?

Yes, Its possible.

Consider we want to query all students belonging to a particular department whose study particular subject. (Note: we have stored subjects as list). Here is how we can achieve it

def filter_stud_with_subject_list_contains(dept, subject_name):
dynamodb = boto3.resource('dynamodb',
aws_access_key_id="dummy",
aws_secret_access_key="dummy",
region_name="local",
endpoint_url="http://localhost:8000")

table = dynamodb.Table('Student')

response = table.query(
KeyConditionExpression=Key('dept').eq(dept),
FilterExpression=Attr('subjects').contains(subject_name)

)

print(response['Items'])

Irrespective of number of items coming in response, response[‘Items’] will always return a list

If there is no data matching given condition, we will get empty list []

Thats all about Query Items from DynamoDB !

Next article in this seriesAWS DynamoDB with Python (Boto3) — Part 4 — Update Attribute & Delete Item from DynamoDB

GitHub Repo URL — https://github.com/iamvickyav/dynamodb-with-python-boto3

--

--