1
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?

DynamoDB AWS CLI コマンド&制約 早見表

Posted at

はじめに

組織内でDynamoDBを使用する事が推奨されるようになったのでまずはCLIで操作してみてよく使いそうなものや、制約をまとめてみました。

対象者

この記事は下記のような人を対象にしています。

  • 駆け出しエンジニア
  • プログラミング初学者
  • DynamoDBをCLIで操作してみたい方

データ操作

操作 コマンド 詳細情報
アイテム追加 put-item ドキュメント
複数アイテム追加 batch-write-item ドキュメント
アイテム取得 get-item ドキュメント
複数アイテム取得 batch-get-item ドキュメント
アイテム更新 update-item ドキュメント
アイテム削除 delete-item ドキュメント
アイテム検索 query ドキュメント
アイテム全件取得 scan ドキュメント

put-item

aws dynamodb put-item \
    --table-name YourTableName \
    --item '{"PrimaryKey": {"S": "example-pk"}, "SortKey": {"S": "example-sk"}, "Attribute": {"S": "value"}}'

batch-write-item

注意点と制約

  • 最大アイテム数:最大25件のリクエスト、または合計16MBまでのデータを一度に操作可能

    これを超えるリクエストは分割して送信する必要があります。
  • 容量消費:読み取りキャパシティーユニット(RCU)は取得した合計のデータ容量でRCUは計算されず、1項目毎にRCUは消費される

1.アイテムの一括追加

request-items.json

{
  "YourTableName": [
    {
      "PutRequest": {
        "Item": {
          "PrimaryKey": {"S": "item1"},
          "SortKey": {"S": "typeA"},
          "Attribute1": {"S": "Value1"}
        }
      }
    },
    {
      "PutRequest": {
        "Item": {
          "PrimaryKey": {"S": "item2"},
          "SortKey": {"S": "typeB"},
          "Attribute2": {"S": "Value2"}
        }
      }
    }
  ]
}
aws dynamodb batch-write-item --request-items file://batch-write-items.json

2.アイテムの一括削除

request-items.json

{
  "YourTableName": [
    {
      "DeleteRequest": {
        "Key": {
          "PrimaryKey": {"S": "item1"},
          "SortKey": {"S": "typeA"}
        }
      }
    },
    {
      "DeleteRequest": {
        "Key": {
          "PrimaryKey": {"S": "item2"},
          "SortKey": {"S": "typeB"}
        }
      }
    }
  ]
}
aws dynamodb batch-write-item --request-items file://batch-write-items.json

3.追加と削除を混在させる

request-items.json

{
  "YourTableName": [
    {
      "PutRequest": {
        "Item": {
          "PrimaryKey": {"S": "item3"},
          "SortKey": {"S": "typeC"},
          "Attribute3": {"S": "Value3"}
        }
      }
    },
    {
      "DeleteRequest": {
        "Key": {
          "PrimaryKey": {"S": "item1"},
          "SortKey": {"S": "typeA"}
        }
      }
    }
  ]
}
aws dynamodb batch-write-item --request-items file://batch-write-items.json

get-item

aws dynamodb get-item \
    --table-name YourTableName \
    --key '{"PrimaryKey": {"S": "example-pk"}, "SortKey": {"S": "example-sk"}}'

batch-get-item

注意点と制約

  • 最大アイテム数:BatchGetItem は、最大100件のアイテムまたは16MBのデータを一度に取得できます
  • 容量消費:読み取りキャパシティーユニット(RCU)は取得した合計のデータ容量でRCUは計算されず、1項目毎にRCUは消費される
  • 部分取得の可能性:リクエストが大きすぎたり、DynamoDBのスロットリングが発生した場合、一部のキーが UnprocessedKeys に戻されることがあります。この場合、未処理のキーを再試行する必要があります

単一のテーブルから複数アイテムを取得

request-items.json

{
  "YourTableName": {
    "Keys": [
      {
        "PrimaryKey": {"S": "item1"},
        "SortKey": {"S": "typeA"}
      },
      {
        "PrimaryKey": {"S": "item2"},
        "SortKey": {"S": "typeB"}
      }
    ]
  }
}
aws dynamodb batch-get-item --request-items file://request-items.json

複数のテーブルから取得

request-items.json

{
  "YourTableName": {
    "Keys": [
      {
        "PrimaryKey": {"S": "item1"},
        "SortKey": {"S": "typeA"}
      }
    ]
  },
  "AnotherTableName": {
    "Keys": [
      {
        "PrimaryKey": {"S": "item3"}
      }
    ]
  }
}
aws dynamodb batch-get-item --request-items file://request-items.json

update-item

aws dynamodb update-item \
    --table-name YourTableName \
    --key '{"PrimaryKey": {"S": "example-pk"}, "SortKey": {"S": "example-sk"}}' \
    --update-expression "SET #attr = :val" \
    --expression-attribute-names '{"#attr": "Attribute"}' \
    --expression-attribute-values '{":val": {"S": "new-value"}}'

delete-item

aws dynamodb delete-item \
    --table-name YourTableName \
    --key '{"PrimaryKey": {"S": "example-pk"}, "SortKey": {"S": "example-sk"}}'

query

注意点と制約

  • 最大アイテム数:BatchGetItem は、最大 1 MBのデータを一度に取得できます
  • 容量消費:読み取りキャパシティーユニット(RCU)は取得した合計のデータ容量に対してRCUは計算され消費される
  • 途中まで取得の可能性:取得するデータ合計が1MBより大きいと、取得出来なくなった最初のキーが lastEvaluatedKey に戻されることがあります。この場合、未処理のキーから再試行する必要があります

テーブルに設定されているプライマリーキーを使用した取得

aws dynamodb query \
    --table-name YourTableName \
    --key-condition-expression "PrimaryKey = :pk" \
    --expression-attribute-values '{":pk": {"S": "example-pk"}}'

GSIで設定されているパティションキーを使用した取得

aws dynamodb query \
    --table-name YourTableName \
    --index-name YourIndexName \
    --key-condition-expression "GSI_Key = :key" \
    --expression-attribute-values '{":key": {"S": "example-key"}}'

scan

注意点と制約

  • 容量消費:読み取りキャパシティーユニット(RCU)は取得した合計のデータ容量に対してRCUは計算され消費される
aws dynamodb scan --table-name YourTableName

おわりに

AWS SDKを見てみるとCLIとかなり近しい形だったので一度触ってみてもいいなと思いました。

参考記事

AWS CLI
AWS SDK for Java 2.x

1
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
1
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?