AWS DynamoDB snippets

How to get item in DynamoDB with Python boto3 client

Please note that this snippet is part of the DynamoDB-Simpsons-episodes-full-example repository on GitHub.

Let's say you want to read the data from a DynamoDB table using the Primary Key of your simpsonsEpisodes table.

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.

TABLENAME='simpsonsEpisodes'

The next step is to define a specific script to extract an item with particular valut of HASH key and SORT key.

response = dynamodbClient.get_item(
    TableName=TABLENAME,
    Key={
        'No_Season': {'N': '1'},
        'No_Inseason': {'N': '8'}
    }
)

The response code and the data of the item are stored in the response variable.

print(response['Item'])
{'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'}}

Back to AWS DynamoDB cookbook page