Amazon DynamoDB is a key-value and document database that delivers single-digit millisecond performance at any scale. https://aws.amazon.com/dynamodb/
This guide is meant to provide a base, or a quick start, for interfacing with AWS DynamoDB using Python, its contents are based on the repo https://github.com/5thempire/aws-dynamodb.
The chosen examples are meant to illustrate the breadth of options available, even though they’re not the typical use case.
We are going to create two tables: one for storing URLs and their corresponding domain, and another for storing logs, which have a timestamp, a log status and a message. The URLs data model will be using a key-value configuration, while the logs data model will use a document configuration.
First things first, let’s define the data models.
The key-value configuration is a very simple implementation, where the only requirement is to specify the key or index. In DynamoDB this is set under the attribute definition and the key schema.
The attribute definition stores the name of the keys and their corresponding type.
The key schema defines the type of primary keys we’re using, in DynamoDB they can be one of two types, hash or range. We could have one of each, but for the current example we’ll only be using hash, leaving out range for you to play with.
The document configuration is a bit more complex. To query the log documents we’ll use two indexes, one will be the timestamp and the other the status of the log, the latter of which is set as a global secondary index.
The global secondary index also includes a projection attribute. This configuration is very important, since it defines what will be retrieved when we query using the secondary index. In our definition we’re retrieving the complete document.
This approach has one base class to interact with DynamoDB, which is not meant to be used on its own, but to provide a solid base for the table specific definitions. These specificities are set by overriding the abstract methods for get, put, update and remove.
This class overrides the base class by defining what’s specific to the domain data model interaction.
This class overrides the base class by defining what’s specific to the log data model interaction, but also adds filtering methods. There are two filters defined:
- filter_by_timestamp_status : which uses both timestamp and status keys to filter results
- filter_by_status : which filters by a specific status (i.e.: INFO)
Don’t forget there are a few options out there that provide the typical level of abstraction a database model normally requires. However, sometimes a more hands on approach is preferred.
For those who want to experience a running example visit https://github.com/5thempire/aws-dynamodb. There you’ll find the samples mentioned here along with the corresponding set of tests, that can be used as a basis for further experiments.
At Runtime Revolution we take our craft seriously and always go the extra mile to deliver a reliable, maintainable, and testable product. Do you have a project to move forward or a product you’d like to launch? We would love to help you!