Amazon DynamoDB Basics
Introduction
Introduction
Amazon DynamoDB is a fully managed NoSQL database service offered by Amazon Web Services (AWS). It provides fast and predictable performance with seamless scalability. If you’re looking to get started with DynamoDB, this tutorial will guide you through the basic concepts and operations.
Imagine you’re building a personalized app. Users love customization, and you decide to offer them the ability to save their preferences. You need a database that’s fast, scalable, and flexible. In this tutorial, we’ll create a table in DynamoDB which stores user preferences.
Prerequisites:
- An AWS account.
- AWS CLI installed (optional but recommended for this tutorial).
- Basic understanding of databases.
Key Concepts
- Hash Key: This is the unique key that identifiers a record in the database. A hash key (sometimes called a partition key) is used by DynamoDB to partition your data. The most efficient way to retrieve records is to retrieve it using the hash key.
- Sort Key: You can have multiple records with a single hash key as long as you also provide a sort key. This is used to retrieve data from the database sorted. This is useful if you’re trying to find the most recent event in a table, or the latest version of a record.
Create a DynamoDB Table
To get started with DynamoDB, you’ll need to create a table. For this tutorial, we’ll create a new table called UserPreferences
which will store a user ID and a preferred color.
First log into the AWS Management Console. Navigate to DynamoDB through the “Services” dropdown. Select the region where you’d like your database to reside in the top-right corner of the screen.
On the DynamoDB console, you’ll be greeted with a “Create table” button. Click this button to create a new DynamoDB table. Name the table “UserPreferences”.
Basic Operations
We’ll construct our UserPreferences
table to have a hash key Username
and a single attribute Color
which stores the user’s preferred color. For each the following basic operations, we’re going to create, retrieve, update, and delete data for a user with the Username
of johndoe
who’s favorite color is orange
.
Inserting Data
Use the AWS Management Console or AWS CLI to insert data:
aws dynamodb put-item --table-name UserPreferences --item '{ "Username": {"S": "johndoe"}, "Color": {"S": "Orange"}}'
Retrieving Data
Retrieve an item using its primary key:
aws dynamodb get-item --table-name UserPreferences --key '{ "Username": {"S": "johndoe"}}'
Updating Data
Update an item’s attribute using the update-item
command.
aws dynamodb update-item --table-name UserPreferences --key '{ "Username": {"S": "johndoe"}}' --update-expression "SET Color = :newvalue" --expression-attribute-values '{ ":newvalue": {"S": "red"}}'
Note that we are creating an update expression. This means we are telling DynamoDB to set the attribute Color
to be equal to the attribute value we define as :newvalue
. In our --expression-attribute-values
block, we describe :newvalue
to equal red
.
Deleting Data
Delete an item using its primary key:
aws dynamodb delete-item --table-name UserPreferences --key '{ "Username": {"S": "johndoe"}}'
Scans and Queries
There are 2 methods for retrieving data from a table: Scans and Queries. In most production use cases, you will use a query as it is more efficient.
Scan Operations
Scans return all the items in a table. Use sparingly as they can consume many resources:
aws dynamodb scan --table-name UserPreferences
This will scan the entire table and return all items from the table. Although this works when we only have 1 user in our table, as our table grows to thousands (or millions) of users, this operation will become slower over time.
Query Operations
Queries retrieve items based on the primary key:
aws dynamodb query --table-name UserPreferences --key-condition-expression "Username = :value" --expression-attribute-values '{":value": {"S": "johndoe"}}'
Similar to our update command above, this query expression tells DynamoDB to retrieve all items where the Username
is equivalent to the value we provided as :value
in our --expression-attribute-values
block. If we use a sort key, then this will query for all items matching the hash key. If there is no sort key, then this query operation will retrieve a single item.
Cleanup
Remember, AWS services cost money. When you’re done experimenting:
- Navigate to the DynamoDB section in the AWS Management Console.
- Select your table.
- Click on the “Delete table” button.
Conclusion
Amazon DynamoDB is a powerful NoSQL database that offers flexibility, scalability, and reliability. This tutorial covered the basics, but DynamoDB has many more features to explore, such as streams, secondary indexes, and integrated caching with DAX. As you get more comfortable, you can delve deeper into these advanced features and fine-tune your DynamoDB experience!