LoginSignup
0
0

More than 1 year has passed since last update.

AWS DynamoDB CLI基礎操作:データ更新(update-expression)

Last updated at Posted at 2022-06-08

AWS コマンド確認

まずはAWS CLIで使うことができるコマンドを確認します。

aws dynamodb help

このようにコマンド一覧が表示します。

       o export-table-to-point-in-time

       o get-item

       o help

       o list-backups

       o list-contributor-insights

       o list-exports

       o list-global-tables

       o list-tables

       o list-tags-of-resource

       o put-item

次に、コマンドの利用方法を確認します。
例えば、update-itemコマンドについて知りたい場合

aws dynamodb update-item help

長いので、見たい箇所を探します。

$ aws dynamodb update-item help |grep update
       update-item -
       ues. You can also perform a conditional update on an existing item (in-
            update-item
          [--attribute-updates <value>]
          [--update-expression <value>]
          The name of the table containing the item to update.
          The primary key of the item to be updated. Each element consists  of
                 You  cannot  use  UpdateItem  to  update  any primary key at-
                 Specifies  how  to  perform  the update. Valid values are PUT
                       for  an  item that doesn't exist before the update, Dy-
                       use  ADD  to update an existing item, and intend to in-
                       example, suppose that the item you want to update  does
...

※qを入力するとhelp画面を抜けられます。

更新式 update-expression

DynamoDBの既存のデータを更新するには、update-itemコマンドを利用します。

今回は作成済のProductCatalogテーブルを使用します。
データを追加します。

aws dynamodb put-item \
    --table-name ProductCatalog \
    --item file://item.json

テーブルデータはこのようになります。
item.json

{
    "Id": {"N": "789"},
    "ProductCategory": {"S": "Home Improvement"},
    "Price": {"N": "52"},
    "InStock": {"BOOL": true},
    "Brand": {"S": "Acme"}
}

条件付きで実行する3つのオプション

既存のデータを更新するにはupdate-itemコマンドに条件を付けて実行します。
次の3つのオプションをセットで使います。

  1. UpdateExpression
    データをどう更新するのか記載

  2. ExpressionAttributeNames
    Attributeの名前をKey-Value形式で記載

  3. ExpressionAttributeValues
    値の情報を記載をKey-Value形式で記載

UpdateExpressionができることは4つ

  1. SETアクション: 値を上書きで保存する
  2. REMOVEアクション: Attributeそのものを消す
  3. ADDアクション: 数値の加減算したり、後述するセット型にデータを追加したりする
  4. DELETEアクション: セット型からデータを削除する

今回は既存のデータを上書きしていきます。
「SETアクション」を使用します。

aws dynamodb update-item \
    --table-name ProductCatalog \
    --key '{"Id":{"N":"789"}}' \
    --update-expression "SET ProductCategory = :c, Price = :p" \
    --expression-attribute-values '{ ":c": { "S": "Hardware" }, ":p": { "N": "60" } }' \
    --return-values ALL_NEW
{
    "Attributes": {
        "ProductCategory": {
            "S": "Hardware"
        },
        "Price": {
            "N": "60"
        },
        "Id": {
            "N": "789"
        },
        "InStock": {
            "BOOL": true
        },
        "Brand": {
            "S": "Acme"
        }
    }
}

パーティションキーとソートキーがある場合はkeyに両方記載します。

--key '{ "PK": {"S": "Id"}, "SK": {"S": "Id"}}'

ローカル環境で作業する場合は、エンドポイントURLを指定します。

--endpoint-url http://dynamodb-local:8000

参考サイト

AWS DynamoDBデベロッパーガイド

DynamoDBでデータを更新する際に使うUpdateExpressionについて一通りまとめてみた

AWS CLIでDynamoDB操作

テーブル作成のCLI操作がわかりやすかった:まくろぐ

※今回利用していませんが、GUIアプリ「NoSQL Workbench」で構築することもできます。
公式ガイド>NoSQL Workbench

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