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 Module: 15 Minutes
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.
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 that will be needed in 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 “Application background” section of this tutorial’s “Introduction”, 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.