AWS DynamoDB snippets
How to query data from DynamoDB with Python boto3
Please note that this snippet is part of the DynamoDB-Simpsons-episodes-full-example repository on GitHub.
Let's say you want to query the data from a DynamoDB table using the HASH and SORT keys of simpsonsEpisodes
.
TABLENAME='simpsonsEpisodes'
First of all, you have to create a Client (follow these steps) and then you can use the following code to assign a value to the variables used below.
HASH and SORT keys - equal sign
response = dynamodbClient.query(
TableName=TABLENAME,
KeyConditionExpression='No_Season = :Season AND No_Inseason = :Inseason',
ExpressionAttributeValues={
':Season': {'N': '1'},
':Inseason': {'N': '8'}
}
)
The response code and the data of the item are stored in the response
variable.
print(response['Items'])
[{'Title': {'S': 'The Telltale Head'}, 'ProdCode': {'S': '7G07'}, 'OriginalAirDate': {'S': '1990-02-25'}, 'USViewers(millions)': {'N': '28'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['Rich Moore']}, 'No_Overall': {'N': '8'}, 'WrittenBy': {'SS': ['Al Jean', 'Matt Groening', 'Mike Reiss', 'Sam Simon']}, 'No_Inseason': {'N': '8'}}]
HASH key - equal sign
response = dynamodbClient.query(
TableName=TABLENAME,
KeyConditionExpression='No_Season = :Season',
ExpressionAttributeValues={
':Season': {'N': '1'}
}
)
The response code and the data of the item are stored in the response
variable.
print(response['Items'])
[{'Title': {'S': 'Simpsons Roasting on an Open Fire'}, 'ProdCode': {'S': '7G08'}, 'OriginalAirDate': {'S': '1989-12-17'}, 'USViewers(millions)': {'N': '26.7'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['David Silverman']}, 'No_Overall': {'N': '1'}, 'WrittenBy': {'SS': ['Mimi Pond']}, 'No_Inseason': {'N': '1'}}, {'Title': {'S': 'Bart the Genius'}, 'ProdCode': {'S': '7G02'}, 'OriginalAirDate': {'S': '1990-01-14'}, 'USViewers(millions)': {'N': '24.5'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['David Silverman']}, 'No_Overall': {'N': '2'}, 'WrittenBy': {'SS': ['Jon Vitti']}, 'No_Inseason': {'N': '2'}}, {'Title': {'S': "Homer's Odyssey"}, 'ProdCode': {'S': '7G03'}, 'OriginalAirDate': {'S': '1990-01-21'}, 'USViewers(millions)': {'N': '27.5'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['Wes Archer']}, 'No_Overall': {'N': '3'}, 'WrittenBy': {'SS': ['Jay Kogen', 'Wallace Wolodarsky']}, 'No_Inseason': {'N': '3'}}, {'Title': {'S': "There's No Disgrace Like Home"}, 'ProdCode': {'S': '7G04'}, 'OriginalAirDate': {'S': '1990-01-28'}, 'USViewers(millions)': {'N': '20.2'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['Gregg Vanzo', 'Kent Butterworth']}, 'No_Overall': {'N': '4'}, 'WrittenBy': {'SS': ['Al Jean', 'Mike Reiss']}, 'No_Inseason': {'N': '4'}}, {'Title': {'S': 'Bart the General'}, 'ProdCode': {'S': '7G05'}, 'OriginalAirDate': {'S': '1990-02-04'}, 'USViewers(millions)': {'N': '27.1'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['David Silverman']}, 'No_Overall': {'N': '5'}, 'WrittenBy': {'SS': ['John Swartzwelder']}, 'No_Inseason': {'N': '5'}}, {'Title': {'S': 'Moaning Lisa'}, 'ProdCode': {'S': '7G06'}, 'OriginalAirDate': {'S': '1990-02-11'}, 'USViewers(millions)': {'N': '27.4'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['Wes Archer']}, 'No_Overall': {'N': '6'}, 'WrittenBy': {'SS': ['Al Jean', 'Mike Reiss']}, 'No_Inseason': {'N': '6'}}, {'Title': {'S': 'The Call of the Simpsons'}, 'ProdCode': {'S': '7G09'}, 'OriginalAirDate': {'S': '1990-02-18'}, 'USViewers(millions)': {'N': '27.6'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['Wes Archer']}, 'No_Overall': {'N': '7'}, 'WrittenBy': {'SS': ['John Swartzwelder']}, 'No_Inseason': {'N': '7'}}, {'Title': {'S': 'The Telltale Head'}, 'ProdCode': {'S': '7G07'}, 'OriginalAirDate': {'S': '1990-02-25'}, 'USViewers(millions)': {'N': '28'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['Rich Moore']}, 'No_Overall': {'N': '8'}, 'WrittenBy': {'SS': ['Al Jean', 'Matt Groening', 'Mike Reiss', 'Sam Simon']}, 'No_Inseason': {'N': '8'}}, {'Title': {'S': 'Life on the Fast Lane'}, 'ProdCode': {'S': '7G11'}, 'OriginalAirDate': {'S': '1990-03-18'}, 'USViewers(millions)': {'N': '33.5'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['David Silverman']}, 'No_Overall': {'N': '9'}, 'WrittenBy': {'SS': ['John Swartzwelder']}, 'No_Inseason': {'N': '9'}}, {'Title': {'S': "Homer's Night Out"}, 'ProdCode': {'S': '7G10'}, 'OriginalAirDate': {'S': '1990-03-25'}, 'USViewers(millions)': {'N': '30.3'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['Rich Moore']}, 'No_Overall': {'N': '10'}, 'WrittenBy': {'SS': ['Jon Vitti']}, 'No_Inseason': {'N': '10'}}, {'Title': {'S': 'The Crepes of Wrath'}, 'ProdCode': {'S': '7G13'}, 'OriginalAirDate': {'S': '1990-04-15'}, 'USViewers(millions)': {'N': '31.2'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['Milton Gray', 'Wes Archer']}, 'No_Overall': {'N': '11'}, 'WrittenBy': {'SS': ['George Meyer', 'John Swartzwelder', 'Jon Vitti', 'Sam Simon']}, 'No_Inseason': {'N': '11'}}, {'Title': {'S': 'Krusty Gets Busted'}, 'ProdCode': {'S': '7G12'}, 'OriginalAirDate': {'S': '1990-04-29'}, 'USViewers(millions)': {'N': '30.4'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['Brad Bird']}, 'No_Overall': {'N': '12'}, 'WrittenBy': {'SS': ['Jay Kogen', 'Wallace Wolodarsky']}, 'No_Inseason': {'N': '12'}}, {'Title': {'S': 'Some Enchanted Evening'}, 'ProdCode': {'S': '7G01'}, 'OriginalAirDate': {'S': '1990-05-13'}, 'USViewers(millions)': {'N': '27.1'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['David Silverman', 'Kent Butterworth']}, 'No_Overall': {'N': '13'}, 'WrittenBy': {'SS': ['Matt Groening', 'Sam Simon']}, 'No_Inseason': {'N': '13'}}]
HASH and SORT keys - between operator
response = dynamodbClient.query(
TableName=TABLENAME,
KeyConditionExpression='No_Season = :Season AND No_Inseason BETWEEN :InseasonStart AND :InseasonEnd',
ExpressionAttributeValues={
':Season': {'N': '1'},
':InseasonStart': {'N': '1'},
':InseasonEnd': {'N': '8'}
}
)
The response code and the data of the item are stored in the response
variable.
print(response['Items'])
[{'Title': {'S': 'Simpsons Roasting on an Open Fire'}, 'ProdCode': {'S': '7G08'}, 'OriginalAirDate': {'S': '1989-12-17'}, 'USViewers(millions)': {'N': '26.7'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['David Silverman']}, 'No_Overall': {'N': '1'}, 'WrittenBy': {'SS': ['Mimi Pond']}, 'No_Inseason': {'N': '1'}}, {'Title': {'S': 'Bart the Genius'}, 'ProdCode': {'S': '7G02'}, 'OriginalAirDate': {'S': '1990-01-14'}, 'USViewers(millions)': {'N': '24.5'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['David Silverman']}, 'No_Overall': {'N': '2'}, 'WrittenBy': {'SS': ['Jon Vitti']}, 'No_Inseason': {'N': '2'}}, {'Title': {'S': "Homer's Odyssey"}, 'ProdCode': {'S': '7G03'}, 'OriginalAirDate': {'S': '1990-01-21'}, 'USViewers(millions)': {'N': '27.5'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['Wes Archer']}, 'No_Overall': {'N': '3'}, 'WrittenBy': {'SS': ['Jay Kogen', 'Wallace Wolodarsky']}, 'No_Inseason': {'N': '3'}}, {'Title': {'S': "There's No Disgrace Like Home"}, 'ProdCode': {'S': '7G04'}, 'OriginalAirDate': {'S': '1990-01-28'}, 'USViewers(millions)': {'N': '20.2'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['Gregg Vanzo', 'Kent Butterworth']}, 'No_Overall': {'N': '4'}, 'WrittenBy': {'SS': ['Al Jean', 'Mike Reiss']}, 'No_Inseason': {'N': '4'}}, {'Title': {'S': 'Bart the General'}, 'ProdCode': {'S': '7G05'}, 'OriginalAirDate': {'S': '1990-02-04'}, 'USViewers(millions)': {'N': '27.1'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['David Silverman']}, 'No_Overall': {'N': '5'}, 'WrittenBy': {'SS': ['John Swartzwelder']}, 'No_Inseason': {'N': '5'}}, {'Title': {'S': 'Moaning Lisa'}, 'ProdCode': {'S': '7G06'}, 'OriginalAirDate': {'S': '1990-02-11'}, 'USViewers(millions)': {'N': '27.4'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['Wes Archer']}, 'No_Overall': {'N': '6'}, 'WrittenBy': {'SS': ['Al Jean', 'Mike Reiss']}, 'No_Inseason': {'N': '6'}}, {'Title': {'S': 'The Call of the Simpsons'}, 'ProdCode': {'S': '7G09'}, 'OriginalAirDate': {'S': '1990-02-18'}, 'USViewers(millions)': {'N': '27.6'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['Wes Archer']}, 'No_Overall': {'N': '7'}, 'WrittenBy': {'SS': ['John Swartzwelder']}, 'No_Inseason': {'N': '7'}}, {'Title': {'S': 'The Telltale Head'}, 'ProdCode': {'S': '7G07'}, 'OriginalAirDate': {'S': '1990-02-25'}, 'USViewers(millions)': {'N': '28'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['Rich Moore']}, 'No_Overall': {'N': '8'}, 'WrittenBy': {'SS': ['Al Jean', 'Matt Groening', 'Mike Reiss', 'Sam Simon']}, 'No_Inseason': {'N': '8'}}]
HASH and SORT keys - less than operator
response = dynamodbClient.query(
TableName=TABLENAME,
KeyConditionExpression='No_Season = :Season AND No_Inseason < :Inseason',
ExpressionAttributeValues={
':Season': {'N': '1'},
':Inseason': {'N': '10'}
}
)
The response code and the data of the item are stored in the response
variable.
print(response['Items'])
[{'Title': {'S': 'Simpsons Roasting on an Open Fire'}, 'ProdCode': {'S': '7G08'}, 'OriginalAirDate': {'S': '1989-12-17'}, 'USViewers(millions)': {'N': '26.7'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['David Silverman']}, 'No_Overall': {'N': '1'}, 'WrittenBy': {'SS': ['Mimi Pond']}, 'No_Inseason': {'N': '1'}}, {'Title': {'S': 'Bart the Genius'}, 'ProdCode': {'S': '7G02'}, 'OriginalAirDate': {'S': '1990-01-14'}, 'USViewers(millions)': {'N': '24.5'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['David Silverman']}, 'No_Overall': {'N': '2'}, 'WrittenBy': {'SS': ['Jon Vitti']}, 'No_Inseason': {'N': '2'}}, {'Title': {'S': "Homer's Odyssey"}, 'ProdCode': {'S': '7G03'}, 'OriginalAirDate': {'S': '1990-01-21'}, 'USViewers(millions)': {'N': '27.5'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['Wes Archer']}, 'No_Overall': {'N': '3'}, 'WrittenBy': {'SS': ['Jay Kogen', 'Wallace Wolodarsky']}, 'No_Inseason': {'N': '3'}}, {'Title': {'S': "There's No Disgrace Like Home"}, 'ProdCode': {'S': '7G04'}, 'OriginalAirDate': {'S': '1990-01-28'}, 'USViewers(millions)': {'N': '20.2'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['Gregg Vanzo', 'Kent Butterworth']}, 'No_Overall': {'N': '4'}, 'WrittenBy': {'SS': ['Al Jean', 'Mike Reiss']}, 'No_Inseason': {'N': '4'}}, {'Title': {'S': 'Bart the General'}, 'ProdCode': {'S': '7G05'}, 'OriginalAirDate': {'S': '1990-02-04'}, 'USViewers(millions)': {'N': '27.1'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['David Silverman']}, 'No_Overall': {'N': '5'}, 'WrittenBy': {'SS': ['John Swartzwelder']}, 'No_Inseason': {'N': '5'}}, {'Title': {'S': 'Moaning Lisa'}, 'ProdCode': {'S': '7G06'}, 'OriginalAirDate': {'S': '1990-02-11'}, 'USViewers(millions)': {'N': '27.4'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['Wes Archer']}, 'No_Overall': {'N': '6'}, 'WrittenBy': {'SS': ['Al Jean', 'Mike Reiss']}, 'No_Inseason': {'N': '6'}}, {'Title': {'S': 'The Call of the Simpsons'}, 'ProdCode': {'S': '7G09'}, 'OriginalAirDate': {'S': '1990-02-18'}, 'USViewers(millions)': {'N': '27.6'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['Wes Archer']}, 'No_Overall': {'N': '7'}, 'WrittenBy': {'SS': ['John Swartzwelder']}, 'No_Inseason': {'N': '7'}}, {'Title': {'S': 'The Telltale Head'}, 'ProdCode': {'S': '7G07'}, 'OriginalAirDate': {'S': '1990-02-25'}, 'USViewers(millions)': {'N': '28'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['Rich Moore']}, 'No_Overall': {'N': '8'}, 'WrittenBy': {'SS': ['Al Jean', 'Matt Groening', 'Mike Reiss', 'Sam Simon']}, 'No_Inseason': {'N': '8'}}, {'Title': {'S': 'Life on the Fast Lane'}, 'ProdCode': {'S': '7G11'}, 'OriginalAirDate': {'S': '1990-03-18'}, 'USViewers(millions)': {'N': '33.5'}, 'No_Season': {'N': '1'}, 'DirectedBy': {'SS': ['David Silverman']}, 'No_Overall': {'N': '9'}, 'WrittenBy': {'SS': ['John Swartzwelder']}, 'No_Inseason': {'N': '9'}}]