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