AWS DynamoDB snippets

How to get item in DynamoDB with Python boto3 client and a table index

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 Global Secondary Index AirDateIndex you created on the table 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.

TABLENAME='simpsonsEpisodes'
table = dynamodbRes.Table(TABLENAME)

    response = table.query(
        IndexName="AirDateIndex",
        KeyConditionExpression=Key('OriginalAirDate').eq('1990-02-25'),
    )

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

print(response['Item'])
[{'Title': 'The Telltale Head', 'ProdCode': '7G07', 'OriginalAirDate': '1990-02-25', 'USViewers(millions)': Decimal('28'), 'DirectedBy': {'Rich Moore'}, 'No_Season': Decimal('1'), 'No_Overall': Decimal('8'), 'WrittenBy': {'Mike Reiss', 'Matt Groening', 'Al Jean', 'Sam Simon'}, 'No_Inseason': Decimal('8')}]

Back to AWS DynamoDB cookbook page