Working with DynamoDB in Python

Hello Internet! Been a minute since I last wrote! The other day, I recieved a request to compare item between two tables in DynamoDB and ended up finishing that today. During the code review though, my coworker asked me to do a blog post about working with DynamoDB in Python so this one’s for him.

As usual, we’ll need to first import the library like so

1
2
3
4
5
#!/usr/bin/python
import boto3
from boto3.dynamodb.conditions import Key

dyna = boto3.resource('dynamodb')

Couple of things going on here so let’s examine them:

1
from boto3.dynamodb.conditions import Key

This particular import allows us to use Keys in dynamodb queries, which is going to become very important soon

1
dyna = boto3.resource('dynamodb')

Boto3 is the library for interacting with AWS via python and rightfully so considering everything it’s capable of, so by using boto3.resource(‘dynamodb’) we’re setting dyna to be the dynamodb resource.

Moving on, we can then set up this

1
2
data = dyna.Table('Table_Name')
dbresponse = data.query(IndexName='Index_Name', KeyConditionExpression=Key('Key_Name').eq('Value_To_Search_For'))

This snippet looks deceptive but I assure you, it’s not. Just as Dyna was our pythonic way of using the dynamodb resource, data is the python object equivilent of the table we want to work with.

Next up, we use query() on data to query the table using the specified index, Index_Name, and we are looking for any items where the Key ‘Key_Name’ is equal to the desired value.

Were we using a SQL Database this would be roughly equivilent to SELECT * FROM "Index_Name" WHERE "Key_Name" = "Value_To_Search_For". Please note that I said roughly because DynamoDB is a Key-Value and Document database offering, rather than a SQL Compliant Database like MySQL or PostgreSQL.

We set that query equal to the dbresponse object meaning whatever came back from the query is stored in dbresponse, so let’s take a look at that object shall we?

1
type(dbresponse)
<class 'dict'>

First thing we see is that the dbresponse object is of the Dict class, so let’s check to see what keys are in this Dict

1
print(dbresponse.keys())
dict_keys(['Items', 'Count', 'ScannedCount', 'ResponseMetadata'])

The information we want will be in Items however Count can be useful for finding duplicates as well. One important thing to note with Items is this:

1
type(dbresponse['Items'])
<class 'list'>

It’ll be important to note that down, it’ll also be important to note this:

1
type(dbresponse['Items'][0])
<class 'dict'>

That’s right, it’s a list of dicts, no matter how many items are in Items it’ll always be a list, so long as you remember that, you’ll be just fine. Hopefully with this information you’ll have no problem pythonically working with Dynamodb!