Home

Introduction to Amazon DynamoDB

Overview

Amazon DynamoDB is a fast and flexible NoSQL database service for all applications that need consistent, single-digit millisecond latency at any scale. It is a fully managed database and supports both document and key-value data models. Its flexible data model and reliable performance make it a great fit for mobile, web, gaming, ad-tech, Internet of Things (IoT), and many other applications.

I created a table in DynamoDB to store information about a music library. I queried the music library and then deleted the DynamoDB table.

Topics Covered

Task 1: Create a new table

In this task, I created a new table named Music in DynamoDB. Each table requires a partition key (or a primary key) that is used to partition data across DynamoDB servers. A table can also have a sort key. The combination of a partition key and sort key uniquely identifies each item in a DynamoDB table.

  1. In the AWS Management Console, I chose the Services menu. Under Database, I chose DynamoDB.
  2. I chose Create table.
  3. For the Table name, I entered Music
  4. For the Partition key, I entered Artist and left String selected in the dropdown list.
  5. For Sort key - optional, I entered Song and left String selected.
  6. My table used the default settings for indexes and provisioned capacity.
  7. I scrolled down, and chose Create table.

The table was created in less than 1 minute. I waited for the Music table to be Active before moving on to the next task.

Task 2: Add data

In this task, I added data to the Music table. A table is a collection of data on a particular topic.

Each table contains multiple items. An item is a group of attributes that is uniquely identifiable among all of the other items. Items in DynamoDB are similar in many ways to rows in other database systems. In DynamoDB, there is no limit to the number of items you can store in a table.

Each item consists of one or more attributes. An attribute is a fundamental data element, something that does not need to be broken down any further. For example, an item in a Music table contains attributes such as song and artist. Attributes in DynamoDB are similar columns in other database systems, but each item (row) can have different attributes (columns).

When I wrote an item to a DynamoDB table, only the partition key and sort key (if used) are required. Other than these fields, the table does not require a schema. This means that I could add attributes to one item that may be different than the attributes for other items.

  1. I chose the Music table.
  2. I chose Actions, and then chose Create item.
  3. For the Artist value, I entered Pink Floyd
  4. For the Song value, I entered Money

These were the only required attributes, but I then added additional attributes.

  1. To create an additional attribute, I chose Add new attribute.
  2. In the dropdown list, I selected String.
  3. A new attribute row was added.
  4. For the new attribute, I entered the following:

FIELD: Album
VALUE: The Dark Side of the Moon

  1. To add another new attribute, I chose Add new attribute.
  2. In the dropdown list, I chose Number.
  3. A new number attribute was added.
  4. For the new attribute, I entered the following:

FIELD: Year
VALUE: 1973

  1. I chose Create item.

The item was now added to the Music table.

Similarly, to create a second item, I used the following attributes:

Attribute Name: Artist
Attribute Type: String
Attribute Value: John Lennon

Attribute Name: Song
Attribute Type: String
Attribute Value: Imagine

Attribute Name: Album
Attribute Type: String
Attribute Value: Imagine

Attribute Name: Year
Attribute Type: Number
Attribute Value: 1971

Attribute Name: Genre
Attribute Type: String
Attribute Value: Soft rock

This item had an additional attribute called Genre. This was an example of each item being capable of having different attributes without having to pre-define a table schema.

To create a third item, I used the following attributes:

Attribute Name: Artist
Attribute Type: String
Attribute Value: Psy

Attribute Name: Song
Attribute Type: String
Attribute Value: Gangnam Style

Attribute Name: Album
Attribute Type: String
Attribute Value: Psy 6 (Six Rules), Part 1

Attribute Name: Year
Attribute Type: Number
Attribute Value: 2011

Attribute Name: LengthSeconds
Attribute Type: Number
Attribute Value: 219

Once again, this item had a new LengthSeconds attribute identifying the length of the song. This demonstrated the flexibility of a NoSQL database.

There are also faster ways to load data into DynamoDB, such as using AWS Command Line Interface, programmatically loading data, or using one of the free tools available on the internet.

Task 3: Modify an existing item

I now noticed that there was an error in my data. In this task, I modified an existing item.

  1. In the DynamoDB dashboard, under Tables, I chose Explore Items.
  2. I chose the Music button.
  3. I chose Psy.
  4. I changed the Year from 2011 to 2012.
  5. I chose Save changes.

The item was now updated.

Task 4: Query the table

There are two ways to query a DynamoDB table: query and scan.

A query operation finds items based on the primary key and optionally the sort key. It is fully indexed, so it runs very fast.

  1. I expanded Scan/Query items, and chose Query.
  2. Fields for the Artist (Partition key) and Song (Sort key) were now displayed.
  3. I entered the following details:

Artist (Partition key): Psy
Song (Sort key): Gangnam Style

  1. I chose Run.

The song quickly appeared in the list. I needed to scroll down to see this result.

A query is the most efficient way to retrieve data from a DynamoDB table.

Alternatively, I could scan for an item. This option involves looking through every item in a table, so it is less efficient and can take significant time for larger tables.

  1. I scrolled up on the page, and chose Scan.
  2. I expanded Filters, and entered the following values:

For Attribute name, enter Year
For Type, choose Number.
For Value, enter 1971

  1. I chose Run

Only the song released in 1971 was displayed.

Task 5: Delete the table

In this task, I deleted the Music table, which also deleted all the data in the table.

  1. In the DynamoDB dashboard, under Tables, I chose Update settings.
  2. I chose the Music table if it was not already selected.
  3. I chose Actions, and then chose Delete table.
  4. On the confirmation panel, I entered delete and chose Delete table.

The table was deleted.

Conclusion

I have now successfully:

Related Topics