The course is part of these learning pathsSee 3 more
Creating DynamoDB Tables
Reading and Writing Data
Queries and Scans
Working with Large Tables
This course provides an introduction to working with Amazon DynamoDB, a fully-managed NoSQL database service provided by Amazon Web Services. We begin with a description of DynamoDB and compare it to other database platforms. The course continues by walking you through designing tables, and reading and writing data, which is somewhat different than other databases you may be familiar with. We conclude with more advanced topics including secondary indexes and how DynamoDB handles very large tables.
You will gain the following skills by completing this course:
- How to create DynamoDB tables.
- How to read and write data.
- How to use queries and scans.
- How to create and query secondary indexes.
- How to work with large tables.
You should take this course if you have:
- An understanding of basic AWS technical fundamentals.
- Awareness of basic database concepts, such as tables, rows, indexes, and queries.
- A basic understanding of computer programming. The course includes some programming examples in Python.
See the Intended Audience section.
This Course Includes
- Expert-guided lectures about Amazon DynamoDB.
- 1 hour and 31 minutes of high-definition video.
- Expert-level instruction from an industry veteran.
What You'll Learn
|Video Lecture||What You'll Learn|
|DynamoDB Basics||A basic and foundational overview of DynamoDB.|
|Creating DynamoDB Tables||How to create DynamoDB tables and understand key concepts.|
|Reading and Writing Data||How to use the AWS Console and API to read and write data.|
|Queries and Scans||How to use queries and scans with the AWS Console and API.|
|Secondary Indexes||How to work with Secondary Indexes.|
|Working with Large Tables||How to use partitioning in large tables.|
If you have thoughts or suggestions for this course, please contact Cloud Academy at email@example.com.
Let's get started writing some code. This tutorial should help give you an idea of common DynamoDB API methods, no matter what language you're working with. As I mentioned earlier, we're using Python for these tutorials, along with Amazon's SDK for Python called Boto3. You may not be using Python yourself. We're just using this SDK as an example. This lesson will have a little bit of boilerplate Python that's safe to ignore.
Let's open an interactive Python shell and get started. We're going to start with a little bit of boilerplate to connect to DynamoDB. Once we have that in place, we can start actually working with the Python SDK. Boto3 uses Table objects, and we need one Table object to work with data in each table. Let's create a Table object for our Orders table.
We'll start by looking up a single order, the same thing we did in the console. The API method for that is called GetItem. It requires two parameters, the name of the table to load the data from and the primary key of the item to load. But because we're working with Table objects, we don't have to specify the name of the table ourselves. So let's go ahead and call GetItem, including the primary key.
You'll see this command didn't actually work, because we have to be more careful in how we provide the key parameter. Although this table uses a single primary key field, the DynamoDB API expects us to send a dictionary or a map with the name of the field as well as its value. Let's make that change and try running this again.
That time it looks more promising. Behind the scenes, the Boto library made a call to the API's GetItem method. It actually did send both parameters, the table name, and the key that we specified ourselves. Let's print out the response that we got from the DynamoDB API.
We can see that it returned a single item, and we can see all of the fields in that item. We can also see some of the metadata that comes from the HTTP response, like the HTTP status code and Amazon's request ID.
Now, let's try using GetItem to return data from our Ordered Line Items table. Let's create a Table object that can read from that table, and let's try calling GetItem on this table like we did before to get the line items in this order. This returns an error. The provided key element does not match the schema. That's because this table uses a composite primary key with two fields, but we only specified one of them. GetItem can only be used to retrieve one record at a time. The only way we could use this would be to add a line number to the key and pull a single record. Let's try that. Ah-ha. This time it worked and we can see the data about that one item.
Now, let's look at how to insert a new record. For this we'll use the PutItem API method, which takes the item's attributes as its parameter. So let's start with the order details as a dictionary or a map. Now we just need to call PutItem to store it in the table. You'll see that the PutItem call appeared to work, but when we examine the response we didn't really get much output other than the HTTP status code 200 (OK). Let's call the GetItem method to load this order from the table and make sure it looks right. Yep, there it is.
What if we want to make changes to an existing record? The UpdateItem API method will help with that. Let's say that the order had the wrong ZIP Code and we want to adjust it. Well, we can call UpdateItem like this. Here we have to use an update expression, which looks a little bit like SQL. It defines what attributes to update and what actions should be performed on them. Instead of "Set," we can say "Add" to add a new attribute to the record, or "Remove" to delete an existing attribute. In this case, we are modifying the Shipping ZIP attribute, and we need to provide the new value that we're storing in there in the parameter called ExpressionAttributeValues. You can see that this worked without any problems. Let's double-check that the new ZIP Code is in the database. There it is, 54999.
Finally, let's remove this order from the database. That one's easy to do too. The API method we will use is called DeleteItem. Again, we can see that it worked and returned status code 200 (OK). So that's how to do some basic data access using the DynamoDB API. If you're using Java or PHP, or any other language, you'll be working with these same API methods even if your client library looks a bit different.
About the Author
Ryan is the Storage Operations Manager at Slack, a messaging app for teams. He leads the technical operations for Slack's database and search technologies, which use Amazon Web Services for global reach.
Prior to Slack, Ryan led technical operations at Pinterest, one of the fastest-growing social networks in recent memory, and at Runscope, a debugging and testing service for APIs.
Ryan has spoken about patterns for modern application design at conferences including Amazon Web Services re:Invent and O'Reilly Fluent. He has also been a mentor for companies participating in the 500 Startups incubator.