Get started with DynamoDB with docker

Basic things

First, set up the container. Let's set the port and name.

$ docker run -p 8000:8000 --name sample-dynamo amazon/dynamodb-local:1.13.1
Initializing DynamoDB Local with the following configuration:
Port:	8000
InMemory:	true
DbPath:	null
SharedDb:	false
shouldDelayTransientStatuses:	false
CorsParams:	*

First, check the table of the created container. I'm running it on my local 8000, so I'll do the following with --endpoint-url.

$ aws dynamodb list-tables --endpoint-url http://localhost:8000
{
    "TableNames": []
}

It turns out that the table does not exist.

Next, make a table. Be careful not to use Reserved Words (https://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/ReservedWords.html) for AttributeName.

$ aws dynamodb create-table \
  --table-name users \
  --attribute-definitions AttributeName=nickname,AttributeType=S \
  --key-schema AttributeName=nickname,KeyType=HASH \
  --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \
  --endpoint-url http://localhost:8000
{
    "TableDescription": {
        "AttributeDefinitions": [
            {
                "AttributeName": "nickname",
                "AttributeType": "S"
            }
        ],
        "TableName": "users",
        "KeySchema": [
            {
                "AttributeName": "nickname",
                "KeyType": "HASH"
            }
        ],
        "TableStatus": "ACTIVE",
        "CreationDateTime": "2020-12-20T18:33:05.236000+09:00",
        "ProvisionedThroughput": {
            "LastIncreaseDateTime": "1970-01-01T09:00:00+09:00",
            "LastDecreaseDateTime": "1970-01-01T09:00:00+09:00",
            "NumberOfDecreasesToday": 0,
            "ReadCapacityUnits": 1,
            "WriteCapacityUnits": 1
        },
        "TableSizeBytes": 0,
        "ItemCount": 0,
        "TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/users"
    }
}

The following commands are used to see and delete the created table.

# describe table
$ aws dynamodb describe-table --table-name users --endpoint-url http://localhost:8000

# delete table
aws dynamodb delete-table --table-name users --endpoint-url http://localhost:8000 

Enter the data and check it

$ aws dynamodb put-item --table-name users --endpoint-url http://localhost:8000 --item '{"nickname": {"S": "taro"}}'
~/D/s/sam-app
$ aws dynamodb scan --table-name users --endpoint-url http://localhost:8000
{
    "Items": [
        {
            "nickname": {
                "S": "taro"
            }
        }
    ],
    "Count": 1,
    "ScannedCount": 1,
    "ConsumedCapacity": null
}

The basic operation is completed.

Composite primary key

aws dynamodb create-table \
--table-name users \
--attribute-definitions AttributeName=nickname,AttributeType=S AttributeName=age,AttributeType=N \
--key-schema AttributeName=nickname,KeyType=HASH AttributeName=age,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \
--endpoint-url http://localhost:8000

Filter when scanning

$ aws dynamodb scan --table-name users --endpoint-url http://localhost:8000 --filter-expression 'nickname = :n' --expression-attribute-values '{":n": {"S": "taro"}}'

$ aws dynamodb scan --table-name users --endpoint-url http://localhost:8000 --filter-expression 'age > :a' --expression-attribute-values '{":a": {"N": "11"}}'

cf. https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LegacyConditionalParameters.ScanFilter.html

Recommended Posts

Get started with DynamoDB with docker
Getting Started with Docker
Get started with Gradle
Maybe it works! Let's get started with Docker!
Build DynamoDB local with Docker
Let's get started with parallel programming
How to get started with slim
[Google Cloud] Getting Started with Docker
Getting Started with Docker with VS Code
How to get started with Gatsby (TypeScript) x Netlify x Docker
I tried to get started with WebAssembly
[Note] How to get started with Rspec
Getting Started with Docker for Mac (Installation)
Let's get started with Java-Create a development environment ①
How to get started with Eclipse Micro Profile
Rails beginners tried to get started with RSpec
Getting Started with DBUnit
Launch MariaDB with Docker
Rails deploy with Docker
Run Pico with docker
Explode Docker with WSL2
Use Puphpeteer with Docker
Getting Started with Ruby
Operate Emby with Docker
Try WildFly with Docker
Use ngrok with Docker
Run Payara with Docker
Getting Started with Swift
[Docker] Connection with MySQL
Php settings with Docker
Getting Started with Doma-Transactions
Disposable PHP with Docker
Install Composer with Docker
Experienced Java users get started with Android application development
I tried to get started with Spring Data JPA
Get started with "Introduction to Practical Rust Programming" (Day 3)
How to get started with creating a Rails app
Get started with serverless Java with the lightweight framework Micronaut!
Proceed with Rust official documentation on Docker container (1. Getting started)
Pytorch execution environment with Docker
Getting Started with Java Collection
Use GDAL with Python with Docker
Run TAO Core with Docker
Docker management with VS Code
Get the URL issued by ngrok with the Docker expose plugin
Set up GitLab with docker
First year Java developers on udemy get started with PHP
Run Rails whenever with docker
Docker autostart settings with wsl2
Getting Started with JSP & Servlet
[Docker] Rails 5.2 environment construction with docker
Spring Boot starting with Docker
Build docker environment with WSL
Version control CocoaPods with Docker
Getting Started with Java Basics
Web application built with docker (1)
Getting Started with Spring Boot
Now is the time to get started with the Stream API
Elasticsearch> Build with docker, get Twitter information and visualize with Kibana
I tried BIND with Docker
React environment construction with Docker