Create and Manage a Nonrelational Database
with Amazon DynamoDB
Module 2: Inserting and Retrieving Data
You will walk through some simple examples of inserting and retrieving data with DynamoDB
Overview
In this lesson, you walk through some simple examples of inserting and retrieving data with DynamoDB. You create your DynamoDB table using the CreateTable API, and then you insert some items using the BatchWriteItem API call. Finally, you retrieve individual items using the GetItem API call. Before you work through these examples, we discuss the data model to be used in your example online bookstore application.
In subsequent modules, you learn how to retrieve multiple items at a time by using the Query API call and how to enable additional query patterns by using secondary indexes. You also see how to update existing items in your table.
Time to complete
15 minutes
Terminology
The following DynamoDB concepts play a key role in this module:
- Table: A collection of DynamoDB data records.
- Item: A single data record in a DynamoDB table. It is comparable to a row in a relational database.
- Attribute: A single data element on an item. It is comparable to a column in a relational database. However, unlike columns in a relational database, attributes do not need to be specified at table creation, other than the primary key discussed later in this module. Attributes can be simple types, such as strings, integers, or Boolean, or they can be complex types, such as lists or maps.
- Primary key: A primary key is a unique identifier for a single item in a DynamoDB table. The primary key name and type must be specified on table creation, and a primary key of the specified type must be included with each item written to a table. A simple primary key consists of a single attribute, and a composite primary key consists of two attributes: a partition key and a sort key. For example, you can create a simple primary key using “UserID” as an identifier, or create a composite primary key by combining “UserID” and “Creation_Date” as an item identifier.
Data model
When building an application, you should always take time to design the data models needed in your application logic. This data model design should consider data access needs of your application, both for reading and writing data.
DynamoDB is a nonrelational database. With nonrelational databases, you don't need to specify the full schema upfront when creating a table. You only need to declare the primary key for your table, which uniquely identifies each record in your table. This reduces the upfront cost of designing your data model because you can easily modify your schema as your application’s needs change.
As mentioned in the previous Application Background module, your application needs to retrieve an individual book by its title and author. Because the combination of title and author is a unique identifier of a book, you can use those attributes as the primary key of your table. Your application also needs to store information about the category of our book, such as history or biography, as well as the available formats of your book — hardcover, paperback, or audiobook — that are mapped to the item numbers in your inventory system.
With these needs in mind, you can use the following schema for your table:
- Title (a string): The title of the book
- Author (a string): The author of the book
- Category (a string): The category of the book, such as history, biography, and sci-fi
- Formats (a map): The different formats that you have available for sale (such as hardcover, paperback, and audiobook) and their item numbers in your inventory system
In the following steps, you create the table by specifying the composite primary key (Author and Title) of your table. Then, you load some items into your table and read individual items from the table.
Implementation
-
Create a DynamoDB table
The directory you downloaded includes a create_table.py script that creates a Books table by using the CreateTable API. You can run this script by entering the following command in the AWS Cloud9 terminal.
python create_table.py
If you open the create_table.py script with the AWS Cloud9 editor, you should notice:
- The script specifies the composite primary key of your table with the KeySchema argument in the CreateTable API call. Your table uses Author as the hash key and Title as the range key.
- The script specifies the provisioned throughput for your table by defining both read capacity units and write capacity units. DynamoDB lets you set read and write capacity separately, allowing you to fine-tune your configuration to meet your application’s needs without paying for costly overprovisioning.
-
Load items into the table
In this step, you load some books into the table. In the AWS Cloud9 terminal, run the following command.
python insert_items.py
This command runs the following script.
import boto3 dynamodb = boto3.resource('dynamodb', region_name='us-east-1') table = dynamodb.Table('Books') with table.batch_writer() as batch: batch.put_item(Item={"Author": "John Grisham", "Title": "The Rainmaker", "Category": "Suspense", "Formats": { "Hardcover": "J4SUKVGU", "Paperback": "D7YF4FCX" } }) batch.put_item(Item={"Author": "John Grisham", "Title": "The Firm", "Category": "Suspense", "Formats": { "Hardcover": "Q7QWE3U2", "Paperback": "ZVZAYY4F", "Audiobook": "DJ9KS9NM" } }) batch.put_item(Item={"Author": "James Patterson", "Title": "Along Came a Spider", "Category": "Suspense", "Formats": { "Hardcover": "C9NR6RJ7", "Paperback": "37JVGDZG", "Audiobook": "6348WX3U" } }) batch.put_item(Item={"Author": "Dr. Seuss", "Title": "Green Eggs and Ham", "Category": "Children", "Formats": { "Hardcover": "GVJZQ7JK", "Paperback": "A4TFUR98", "Audiobook": "XWMGHW96" } }) batch.put_item(Item={"Author": "William Shakespeare", "Title": "Hamlet", "Category": "Drama", "Formats": { "Hardcover": "GVJZQ7JK", "Paperback": "A4TFUR98", "Audiobook": "XWMGHW96" } })
As the preceding script shows, you used the BatchWriteItem API to load five books into the table. Each book includes the Author and Title attributes for the primary key, and Category and Formats attributes for additional information about the books. Each attribute has a type, which can be a simple type such as a string for the Category attribute, or a complex type such as a map for the Formats attribute.
Note that you inserted data over an HTTP API using the Boto 3 client library. All data access and manipulation requests are done through HTTP requests, rather than maintaining a persistent connection to the database, as is common for relational database management systems.
-
Retrieve items from the table
You can retrieve a single book by using the GetItem API request and specifying the primary key of the item you want.
In the AWS Cloud9 terminal, run the following command.
python get_item.py
This runs the following script to retrieve a single item, which is the book The Rainmaker by John Grisham.
import boto3 dynamodb = boto3.resource('dynamodb', region_name='us-east-1') table = dynamodb.Table('Books') resp = table.get_item(Key={"Author": "John Grisham", "Title": "The Rainmaker"}) print(resp['Item'])
Your terminal should print out the full book data retrieved from the table.
$ python get_item.py {'Title': 'The Rainmaker', 'Formats': {'Hardcover': 'J4SUKVGU', 'Paperback': 'D7YF4FCX'}, 'Author': 'John Grisham', 'Category': 'Suspense'}
Because each item in a table is uniquely identified by its primary key, the GetItem API call will always return at most one item from your table.
In the next module, you learn how to retrieve multiple items from a DynamoDB table with a single API call. You also learn how to enable multiple data access patterns in your table by using secondary indexes.