Skip to main content

DynamoDB Start

DynamoDB is a fully managed NoSQL database service provided by Amazon. It requires only a primary key and doesn’t require a schema to create a table. It can store any amount of data and serve any amount of traffic. With DyanmoDB, you can expect a good performance even when it scales up. It is a very simple and small API that follows key-value method to store, access and perform advanced data retrieval.

DynamoDB comprises of three fundamental units known as table, attribute, and items. A table holds a set of items, an attribute is the simplest element that stores data without any further division and item holds a set of attributes.

DynamoDB use cases

  • Financial service applications
  • Gaming applications
  • Streaming applications

Setting up DynamoDB

  • web service
  • downloadable version

Attribute Data Types

DynamoDB supports a large set of data types for table attributes. Each data type falls into one of the three following categories:

  • Scalar − These types represent a single value, and include number, string, binary, Boolean, and null.
  • Document − These types represent a complex structure possessing nested attributes, and include lists and maps.
  • Set − These types represent multiple scalars, and include string sets, number sets, and binary sets.

Data Types

Partitions

Amazon DynamoDB stores data in partitions. A partition is an allocation of storage for a table, backed by solid state drives (SSDs) and automatically replicated across multiple Availability Zones within an AWS Region. Partition management is handled entirely by DynamoDB—you never have to manage partitions yourself.

When you create a table, the initial status of the table is CREATING. During this phase, DynamoDB allocates sufficient partitions to the table so that it can handle your provisioned throughput requirements. You can begin writing and reading table data after the table status changes to ACTIVE.

Partitions and data distribution

Table, Items, Attributes, Keys, Indexes

  • A table can be visualized as a group of items. Taking an example of Employee records, you will have Employee Name, Employee ID, Address and Phone Number all such items will be stored in a table.
  • An item is a set of attributes in a table. You can also understand an item as a set of attributes that can uniquely define your entry in a table. For example, an item in Employee records will identify a single employee.
  • An attribute is a single field that is attached to an item. E.g. Employee Name.
  • Primary Keys
    • Partition key: This is a simple primary key. If the table has only a partition key, then no two items can have the same partition key value. The primary key must be unique for each item in the table irrespective of the type of primary key that you choose.
    • Composite primary key: This is a combination of partition key and sort key. If the table has a composite primary key, then two items might have the same partition key value. However, those items must have different sort key values.
  • Indexes
  • DynamoDB attributes
    • In Amazon DynamoDB, an item is a collection of attributes. Each attribute has a name and a value. An attribute value can be a scalar, a set, or a document type. For more information, see Amazon DynamoDB: How it works. DynamoDB provides four operations for basic create, read, update, and delete (CRUD) functionality.
    • Working with items and attributes.

DynamoDB Streams

PartiQL statements for DynamoDB

PartiQL Statements
Amazon DynamoDB supports the following PartiQL statements.

info

DynamoDB does not support all PartiQL statements.

This reference provides basic syntax and usage examples of PartiQL statements that you manually run using the AWS CLI or APIs.

Data manipulation language (DML) is the set of PartiQL statements that you use to manage data in DynamoDB tables. You use DML statements to add, modify, or delete data in a table.

The following DML and query language statements are supported:

Basic operations on DynamoDB tables

  • Create a table
  • Write data to a table
  • Read data from a table
  • Update data in a table
  • Delete table
  • Query data in a table

warning

1. LocalStack

awslocal configure (sample)

Run:

awslocal configure

AWS Access Key ID [****************test]:
AWS Secret Access Key [****************test]:
Default region name [test]:
Default output format [json]:

2. dynamodb-admin

Get started with the AWS SDK for Java 2.x

Create a table

AWS Management Console

To create a new Music table using the DynamoDB console:

  • Sign in to the AWS Management Console and open the DynamoDB console at https://console.aws.amazon.com/dynamodb/.
  • In the left navigation pane, choose Tables.
  • Choose Create table.
  • Enter the Table details as follows:
    • For Table name, enter Music.
    • For Partition key, enter Artist.
    • For Sort key, enter SongTitle.
  • Once the table is in ACTIVE status, we recommend that you enable Point-in-time backups for DynamoDB on the table.

Write data to a table

In this step, you insert several items into the table that you created in: Create a table.

AWS Management Console

Follow these steps to write data to the Music table using the DynamoDB console.

  • Open the DynamoDB console at https://console.aws.amazon.com/dynamodb/.
  • In the left navigation pane, choose Tables.
  • On the Tables page, choose the Music table.
  • Choose Explore table items.
  • In the Items returned section, choose Create item.
  • On the Create item page, do the following to add items to your table:
    • Choose Add new attribute, and then choose Number.
    • For Attribute name, enter Awards.
    • Repeat this process to create an AlbumTitle of type String.
    • Enter the following values for your item:
      • For Artist, enter No One You Know.
      • For SongTitle, enter Call Me Today.
      • For AlbumTitle, enter Somewhat Famous.
      • For Awards, enter 1.
  • Choose Create item.
  • Repeat this process and create another item with the following values:
    • For Artist, enter Acme Band.
    • For SongTitle enter Happy Day.
    • For AlbumTitle, enter Songs About Life.
    • For Awards, enter 10.
  • Do this one more time to create another item with the same Artist as the previous step, but different values for the other attributes:
    • For Artist, enter Acme Band.
    • For SongTitle enter PartiQL Rocks. For AlbumTitle, enter Another Album Title. For Awards, enter 8.

Read data from a table

In this step, you'll read back one of the items that you created in Write data to a table.

AWS Management Console

  • Open the DynamoDB console at https://console.aws.amazon.com/dynamodb/.
  • In the left navigation pane, choose Tables.
  • On the Tables page, choose the Music table.
  • Choose Explore table items.
  • On the Items returned section, view the list of items stored in the table, sorted by Artist and SongTitle. The first item in the list is the one with the Artist named Acme Band and the SongTitle PartiQL Rocks.

Update data in a table

In this step, you update an item that you created in Write data to a table.

AWS Management Console

  • Open the DynamoDB console at https://console.aws.amazon.com/dynamodb/.
  • In the left navigation pane, choose Tables.
  • Choose the Music table from the table list.
  • Choose Explore table items.
  • In Items returned, for the item row with Acme Band Artist and Happy Day SongTitle, do the following:
    • Place your cursor on the AlbumTitle named Songs About Life.
    • Choose the Edit icon.
    • In the Edit String popup window, enter Songs of Twilight.
    • Choose Save.
    info

    Alternatively, to update an item, do the following in the Items returned section:

    • Choose the item row with Artist named Acme Band and SongTitle named Happy Day.
    • From the Actions dropdown list, choose Edit item.
    • For enter AlbumTitle, enter Songs of Twilight.
    • Choose Save and close.

Delete table

AWS Management Console

To delete the Music table using the console:

Query data in a table

In this step, you query the data that you wrote to the Music table in Write data to a table by specifying Artist. This will display all songs that are associated with the partition key: Artist.

AWS Management Console

Follow these steps to use the DynamoDB console to query data in the Music table.

  • Open the DynamoDB console at https://console.aws.amazon.com/dynamodb/.
  • In the left navigation pane, choose Tables.
  • Choose the Music table from the table list.
  • Choose Explore table items.
  • In Scan or query items, make sure that Query is selected.
  • For Partition key, enter Acme Band, and then choose Run.