0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

DynamoDB に docker で入門する

Last updated at Posted at 2020-12-20

基本的なこと

まずはコンテナを立てます。port と名前を設定しておきましょう。

$ 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:	*

まずは、つくったコンテナのテーブルを調べます。ローカルの 8000 で動かしていますので、--endpoint-url で以下のようにしておきます。

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

テーブルが存在しないことが分かりました。

次にテーブルを作ります。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"
    }
}

作ったテーブルを見てみたり消したりするのは以下のコマンド

# 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 

データを入れて、確認してみる

$ 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
}

以上で基本的な操作ができました。

複合プライマリキー

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

scan するときに filter する

$ 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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?