AWS DynamoDB Basic Usage

DynamoDB Basic Concepts

Amazon DynamoDB is a fully managed NoSQL database service that provides fast, predictable performance and seamless scalability.

Basic structure

DynamoDB has a few core concepts: tables, items, and attributes. A table is a collection of items, and each item is a collection of attributes. You can think of them as the equivalent of collections, documents, and fields in MongoDB.

Primary keys in DynamoDB

Unlike MongoDB, DynamoDB does not generate an _id by default to uniquely identify a record. That is why DynamoDB has the concept of a primary key. You specify the primary key of a table, and the primary key uniquely identifies each item in the table (by designating one attribute as the primary key).

DynamoDB supports two kinds of primary keys:

  • 1. Partition key DynamoDB uses the value of the partition key as the input to an internal hash function. The output of the hash function determines the partition where the item is stored (the physical storage inside DynamoDB).
  • 2. Composite primary key (partition key and sort key) DynamoDB uses the partition key value as the input to an internal hash function. The output of the hash function determines the partition where the item is stored (the physical storage inside DynamoDB). All items with the same partition key are stored together, sorted by the sort key value.

In a table with a partition key and a sort key, two items may have the same partition key value, but they must have different sort key values.

Running DynamoDB locally

  1. Download and extract DynamoDB Local

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html

  1. Run it in the DynamoDB directory (default port 8000)
1
2
3
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

docker run -it -p 8686:8000 amazon/dynamodb-local:latest

AWS CLI

Create table

1
2
3
4
5
6
7
8
aws dynamodb create-table \
    --table-name Music \
    --attribute-definitions \
        AttributeName=Artist,AttributeType=S \
        AttributeName=SongTitle,AttributeType=S \
    --key-schema AttributeName=Artist,KeyType=HASH AttributeName=SongTitle,KeyType=RANGE \
    --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \
    --endpoint-url http://localhost:8686

Upsert item

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
aws dynamodb put-item \
--table-name Music  \
--item \
    '{"Artist": {"S": "No One You Know2"}, "SongTitle": {"S": "Call Me Today2"}, "AlbumTitle": {"S": "Somewhat Famous2"}}' \
--return-consumed-capacity TOTAL  \
--endpoint-url http://localhost:8686

aws dynamodb put-item \
    --table-name Music \
    --item \
    '{"Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"}, "AlbumTitle": {"S": "Songs About Life"}, "Awards": {"N": "10"} }' \
  --return-consumed-capacity TOTAL  \
  --endpoint-url http://localhost:8686

Get item

1
2
3
4
aws dynamodb get-item --consistent-read \
    --table-name Music \
    --key '{ "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"}}' \
  --endpoint-url http://localhost:8686

Update item

1
2
3
4
5
6
7
aws dynamodb update-item \
    --table-name Music \
    --key '{ "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"}}' \
    --update-expression "SET AlbumTitle = :newval" \
    --expression-attribute-values '{":newval":{"S":"Updated Album Title"}}' \
    --return-values ALL_NEW \
  --endpoint-url http://localhost:8686

Query items

1
2
3
4
5
6
7
aws dynamodb query \
    --table-name Music \
    --key-condition-expression "Artist = :name" \
    --expression-attribute-values  '{":name":{"S":"Acme Band"}}' \
  --endpoint-url http://localhost:8686

aws dynamodb query --table-name Music --key-conditions file://key-conditions.json --endpoint-url http://localhost:8686

List tables

1
aws dynamodb list-tables --endpoint-url http://localhost:8686

Describe table

1
aws dynamodb describe-table --table-name Music --endpoint-url http://localhost:8686

Resource limits

1
aws dynamodb describe-limits --endpoint-url http://dynamodb-local:8686 --region us-west-2

Create a Global Secondary Index

1
2
3
4
5
6
7
aws dynamodb update-table \
    --table-name Music \
    --attribute-definitions AttributeName=AlbumTitle,AttributeType=S \
    --endpoint-url http://localhost:8686 \
    --global-secondary-index-updates \
    "[{\"Create\":{\"IndexName\": \"AlbumTitle-index\",\"KeySchema\":[{\"AttributeName\":\"AlbumTitle\",\"KeyType\":\"HASH\"}], \
    \"ProvisionedThroughput\": {\"ReadCapacityUnits\": 10, \"WriteCapacityUnits\": 5      },\"Projection\":{\"ProjectionType\":\"ALL\"}}}]"

Query the Global Secondary Index

1
2
3
4
5
6
aws dynamodb query \
--table-name Music \
--index-name AlbumTitle-index \
--key-condition-expression "AlbumTitle = :name" \
--expression-attribute-values  '{":name":{"S":"Somewhat Famous"}}' \
--endpoint-url http://localhost:8686

Delete table

1
aws dynamodb delete-table --table-name Music --endpoint-url http://localhost:8686
CC BY-NC-ND 4.0