AWS CLIを利用したDynamoDBの操作方法を確認します。「テーブル作成・削除」「アイテムの挿入・取得・更新・削除」について動作確認します。
目次
helpで確認
利用できるコマンドの確認
DynamoDBの操作で利用できるコマンドを確認したい場合、下記コマンドを実行します。
aws dynamodb help
以下のコマンドが実行できるようです。
batch-get-item
batch-write-item
create-backup
create-global-table
create-table
delete-backup
delete-item
delete-table
describe-backup
describe-continuous-backups
describe-endpoints
describe-global-table
describe-global-table-settings
describe-limits
describe-table
describe-time-to-live
get-item
help
list-backups
list-global-tables
list-tables
list-tags-of-resource
put-item
query
restore-table-from-backup
restore-table-to-point-in-time
scan
tag-resource
transact-get-items
transact-write-items
untag-resource
update-continuous-backups
update-global-table
update-global-table-settings
update-item
update-table
update-time-to-live
wait
各コマンドの利用方法を確認
例えば、queryコマンド
の利用方法を確認したい場合、下記コマンドを実行します。
aws dynamodb query help
テーブル操作
create-table
( テーブル作成 )
usersテーブル
を作成します。
aws dynamodb create-table \
--table-name 'users' \
--attribute-definitions '[{ "AttributeName": "user_id", "AttributeType": "N"}, { "AttributeName": "created_at", "AttributeType": "S" }, { "AttributeName": "post_id", "AttributeType": "N" }]' \
--key-schema '[{ "AttributeName": "user_id", "KeyType": "HASH" }, { "AttributeName": "created_at", "KeyType": "RANGE" }]' \
--local-secondary-indexes '[{ "IndexName": "post_local_index", "Projection": { "ProjectionType": "ALL" }, "KeySchema": [{ "AttributeName": "user_id", "KeyType": "HASH" }, { "AttributeName": "post_id", "KeyType": "RANGE" }]}]' \
--global-secondary-indexes '[{ "IndexName": "post_global_index", "Projection": { "ProjectionType": "ALL" }, "KeySchema": [{ "AttributeName": "post_id", "KeyType": "HASH" }], "ProvisionedThroughput": { "ReadCapacityUnits": 10, "WriteCapacityUnits": 10 }}]' \
--provisioned-throughput '{"ReadCapacityUnits": 10, "WriteCapacityUnits": 10}'
以下、上記処理を実行して生成されたテーブルです。
list-tables
( テーブル一覧 )
$ aws dynamodb list-tables
{
"TableNames": [
"users"
]
}
describe-table
( テーブル詳細 )
$ aws dynamodb describe-table --table-name users
{
"Table": {
"AttributeDefinitions": [
{
"AttributeName": "created_at",
"AttributeType": "S"
},
{
"AttributeName": "post_id",
"AttributeType": "N"
( 省略 )
update-table
( テーブル設定変更 )
変更前の状態
$ aws dynamodb describe-table --table-name users | grep ProvisionedThroughput -A 3
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 10,
"WriteCapacityUnits": 10
--
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 10,
"WriteCapacityUnits": 10
変更
aws dynamodb update-table \
--table-name users \
--provisioned-throughput '{"ReadCapacityUnits": 3, "WriteCapacityUnits": 5}'
変更後の状態
$ aws dynamodb describe-table --table-name users | grep ProvisionedThroughput -A 4
"ProvisionedThroughput": {
"LastDecreaseDateTime": 1558856244.86,
"NumberOfDecreasesToday": 1,
"ReadCapacityUnits": 3,
"WriteCapacityUnits": 5
--
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 10,
"WriteCapacityUnits": 10
},
delete-table
( テーブル削除 )
$ aws dynamodb delete-table --table-name users
{
"TableDescription": {
"TableName": "users",
"TableStatus": "DELETING",
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 3,
"WriteCapacityUnits": 5
},
"TableSizeBytes": 0,
"ItemCount": 0,
( 省略 )
アイテム操作
put-item
( Item挿入 )
usersテーブル
に 5項目
追加してみます。
aws dynamodb put-item --table-name users --item '{ "user_id": { "N": "1" }, "post_id": { "N": "2" }, "created_at": { "S": "1544741492" }, "message": { "S": "aaaaaaaaaaaaaa" } }'
aws dynamodb put-item --table-name users --item '{ "user_id": { "N": "2" }, "post_id": { "N": "9" }, "created_at": { "S": "1544745092" }, "message": { "S": "bbbbbbbbbbbbbb" } }'
aws dynamodb put-item --table-name users --item '{ "user_id": { "N": "3" }, "post_id": { "N": "3" }, "created_at": { "S": "1544748692" }, "message": { "S": "cccccccccccccc" } }'
aws dynamodb put-item --table-name users --item '{ "user_id": { "N": "1" }, "post_id": { "N": "5" }, "created_at": { "S": "1544752292" }, "message": { "S": "dddddddddddddd" } }'
aws dynamodb put-item --table-name users --item '{ "user_id": { "N": "5" }, "post_id": { "N": "3" }, "created_at": { "S": "1544755892" }, "message": { "S": "eeeeeeeeeeeeee" } }'
get-item
( 単一Item取得 )
aws dynamodb get-item \
--table-name users \
--key '{ "user_id": { "N": "1" }, "created_at": { "S": "1544752292" } }'
$ aws dynamodb get-item \
> --table-name users \
> --key '{ "user_id": { "N": "1" }, "created_at": { "S": "1544752292" } }'
{
"Item": {
"message": {
"S": "dddddddddddddd"
},
"user_id": {
"N": "1"
},
"created_at": {
"S": "1544752292"
},
"post_id": {
"N": "5"
}
}
}
query
( 条件に一致するItem取得 )
aws dynamodb query \
--table-name users \
--key-condition-expression 'user_id = :user_id and created_at >= :created_at' \
--expression-attribute-values '{ ":user_id": { "N": "1" }, ":created_at": { "S": "1544752292" } }'
$ aws dynamodb query \
> --table-name users \
> --key-condition-expression 'user_id = :user_id and created_at >= :created_at' \
> --expression-attribute-values '{ ":user_id": { "N": "1" }, ":created_at": { "S": "1544752292" } }'
{
"Items": [
{
"message": {
"S": "dddddddddddddd"
},
"user_id": {
"N": "1"
},
"created_at": {
"S": "1544752292"
},
"post_id": {
"N": "5"
}
}
],
"Count": 1,
"ScannedCount": 1,
"ConsumedCapacity": null
}
scan
( 全Item取得 )
aws dynamodb scan \
--table-name users
$ aws dynamodb scan \
> --table-name users
{
"Items": [
{
"message": {
"S": "cccccccccccccc"
},
"user_id": {
"N": "3"
},
"created_at": {
"S": "1544748692"
},
"post_id": {
"N": "3"
}
},
{
"message": {
"S": "bbbbbbbbbbbbbb"
},
"user_id": {
"N": "2"
},
"created_at": {
"S": "1544745092"
},
"post_id": {
"N": "9"
}
},
{
"message": {
"S": "aaaaaaaaaaaaaa"
},
"user_id": {
"N": "1"
},
"created_at": {
"S": "1544741492"
},
"post_id": {
"N": "2"
}
},
{
"message": {
"S": "dddddddddddddd"
},
"user_id": {
"N": "1"
},
"created_at": {
"S": "1544752292"
},
"post_id": {
"N": "5"
}
},
{
"message": {
"S": "eeeeeeeeeeeeee"
},
"user_id": {
"N": "5"
},
"created_at": {
"S": "1544755892"
},
"post_id": {
"N": "3"
}
}
],
"Count": 5,
"ScannedCount": 5,
"ConsumedCapacity": null
}
scan
( 条件に一致するItem取得 )
aws dynamodb scan \
--table-name users \
--filter-expression 'message = :message' \
--expression-attribute-values '{ ":message": { "S": "cccccccccccccc" } }'
$ aws dynamodb scan \
> --table-name users \
> --filter-expression 'message = :message' \
> --expression-attribute-values '{ ":message": { "S": "cccccccccccccc" } }'
{
"Items": [
{
"message": {
"S": "cccccccccccccc"
},
"user_id": {
"N": "3"
},
"created_at": {
"S": "1544748692"
},
"post_id": {
"N": "3"
}
}
],
"Count": 1,
"ScannedCount": 5,
"ConsumedCapacity": null
}
update-item
( Item更新 )
aws dynamodb update-item \
--table-name users \
--key '{ "user_id": { "N": "3" }, "created_at": { "S": "1544748692" } }' \
--update-expression 'set message = :message, post_id=:post_id' \
--expression-attribute-values '{ ":message": { "S": "update_xxxxxxxxxxxxxxx" }, ":post_id": { "N": "100" } }' \
--return-values 'ALL_NEW'
$ aws dynamodb update-item \
> --table-name users \
> --key '{ "user_id": { "N": "3" }, "created_at": { "S": "1544748692" } }' \
> --update-expression 'set message = :message, post_id=:post_id' \
> --expression-attribute-values '{ ":message": { "S": "update_xxxxxxxxxxxxxxx" }, ":post_id": { "N": "100" } }' \
> --return-values 'ALL_NEW'
{
"Attributes": {
"message": {
"S": "update_xxxxxxxxxxxxxxx"
},
"user_id": {
"N": "3"
},
"created_at": {
"S": "1544748692"
},
"post_id": {
"N": "100"
}
}
}
delete-item
( Item削除 )
aws dynamodb delete-item \
--table-name users \
--key '{ "user_id": { "N": "1" }, "created_at": { "S": "1544752292" } }'
$ aws dynamodb scan \
> --table-name users \
> --select "COUNT"
{
"Count": 5,
"ScannedCount": 5,
"ConsumedCapacity": null
}
$
$
$ aws dynamodb delete-item \
> --table-name users \
> --key '{ "user_id": { "N": "1" }, "created_at": { "S": "1544752292" } }'
$
$
$ aws dynamodb scan \
> --table-name users \
> --select "COUNT"
{
"Count": 4,
"ScannedCount": 4,
"ConsumedCapacity": null
}