Skip to content

noahingh/dynamodb-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Dynamodb tutorial

What is covered?

Index

  1. Set up
  2. Create a Table
  3. Writing Data a Table
  4. Reading Data from a Table
  5. Managing Indexes

Set up

Set up with Dynamodb docker image.

# run dynamodb service.
$ docker run -p 8000:8000 amazon/dynamodb-local:1.12.0 \
    -jar DynamoDBLocal.jar -inMemory  -sharedDb
# set up for the client and test the connection with the Docker container.
$ export AWS_ACCESS_KEY_ID=key; export AWS_SECRET_ACCESS_KEY=secret; export AWS_DEFAULT_REGION=ap-northeast-1;
$ export ENDPOINT=http://localhost:8000/

$ aws dynamodb list-tables --endpoint-url $ENDPOINT
{
    "TableNames": []
}

Create a Table

# create the Music table.
$ aws dynamodb create-table \
    --endpoint $ENDPOINT \
    --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 \

$ aws dynamodb list-tables --endpoint-url $ENDPOINT
{
    "TableNames": [
        "Music"
    ]
}

Getting information

$ aws dynamodb describe-table --table-name Music --endpoint $ENDPOINT

{
    "Table": {
        "AttributeDefinitions": [
            {
                "AttributeName": "Artist",
                "AttributeType": "S"
            },
            {
                "AttributeName": "SongTitle",
                "AttributeType": "S"
            }
        ],
        "TableName": "Music",
        "KeySchema": [
            {
                "AttributeName": "Artist",
                "KeyType": "HASH"
            },
            {
                "AttributeName": "SongTitle",
                "KeyType": "RANGE"
            }
        ],
        ...
    }
}

Writing data

Seed datas same as doc.

$ sh scripts/seed.sh

Reading data

There are three options to read data in DynamoDB(doc).

GetItem

Retrieves a single item from a table.

# the entire item with all of its attributes.
$ aws dynamodb get-item --endpoint $ENDPOINT \
    --consistent-read \
    --table-name Music \
    --key \
        '{ 
            "Artist": {"S": "No One You Know"}, 
            "SongTitle": {"S": "Call Me Today"}
        }'

# add a ProjectionExpression parameter to return only some of the attributes.
$ aws dynamodb get-item --endpoint $ENDPOINT \
    --consistent-read \
    --table-name Music \
    --projection-expression "AlbumTitle,Price" \
    --key \
        '{ 
            "Artist": {"S": "No One You Know"}, 
            "SongTitle": {"S": "Call Me Today"}
        }'
    

Querying

You can use Query with any table that has a composite primary key (partition key and sort key). You must specify an equality condition for the partition key, and you can optionally provide another condition for the sort key.

KeyConditionExpression parameter specifies the key values that you want to query.

$ aws dynamodb query \
    --endpoint $ENDPOINT \
    --table-name Music \
    --key-condition-expression "Artist = :a and SongTitle = :t" \
    --expression-attribute-values \
        '{
            ":a": {"S":"No One You Know"},
            ":t": {"S":"Call Me Today"}
        }'

# return all of the songs by an artist
$ aws dynamodb query \
    --endpoint $ENDPOINT \
    --table-name Music \
    --key-condition-expression "Artist = :a" \
    --expression-attribute-values \
        '{
            ":a": {"S":"No One You Know"}
        }'

Scaning

Retrieves all of the items, but it support FilterExpression parameter, which you can use to discard items.

Managing indexes

About

Dynamo DB tutorial to cover "from SQL to NoSQL".

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages