0
0

More than 1 year has passed since last update.

DynamoDBのトランザクション処理についてAWS CLIで検証してみた

Posted at

はじめに

業務で、DynamoDBで2つのテーブルを扱う必要がでてきたのでトランザクション処理について動作検証しました。

DynamoDBのトランザクションについて

DynamoDBが公式に機能として提供しています。

CLIの場合は、transact-write-itemsを使うことでデータ更新時のトランザクション処理を実装できるようだったので、ためしてみました。

準備

2つのテーブルを作成
CLI用のアクセスキー取得及び設定
以下コマンドで作成テーブルが参照できることを確認

$ aws dynamodb list-tables --profile sandbox
{
    "TableNames": [
        "Test1",
        "Test2"
    ]
}

検証内容

1: 2つのテーブルの更新が成功する場合(新規追加)

以下コマンドを実行します。

aws dynamodb transact-write-items --profile sandbox --transact-items '[
    {
        "Put": {
            "TableName" : "Test1",
            "Item" : {
                "ID" : {"S": "001"},
                "Point" : {"S": "1"}
            }
        }
    },
    {
        "Put": {
            "TableName" : "Test2",
            "Item" : {
                "ID" : {"S": "001"},
                "TotalPoint" : {"S": "2"}
            }
        }
    }
]'

以下のように、テーブルにデータが追加されています。

$ aws dynamodb scan --table-name Test1 --profile sandbox
{
    "Items": [
        {
            "ID": {
                "S": "001"
            },
            "Point": {
                "S": "1"
            }
        }
    ],
    "Count": 1,
    "ScannedCount": 1,
    "ConsumedCapacity": null
}
$ aws dynamodb scan --table-name Test2 --profile sandbox
{
    "Items": [
        {
            "TotalPoint": {
                "S": "2"
            },
            "ID": {
                "S": "001"
            }
        }
    ],
    "Count": 1,
    "ScannedCount": 1,
    "ConsumedCapacity": null
}

2: 2つのテーブルの更新が成功する場合(更新)

先ほどのコマンドで追加した要素に対して更新してみます。
本来はUpdateを使うべきですが、Putでも同じことができるので、こちらを使用します。

aws dynamodb transact-write-items --profile sandbox --transact-items '[
    {
        "Put": {
            "TableName" : "Test1",
            "Item" : {
                "ID" : {"S": "001"},
                "Point" : {"S": "999"}
            }
        }
    },
    {
        "Put": {
            "TableName" : "Test2",
            "Item" : {
                "ID" : {"S": "001"},
                "TotalPoint" : {"S": "999"}
            }
        }
    }
]'

以下のように、対象IDの属性が更新されることを確認できました。

$ aws dynamodb scan --table-name Test1 --profile sandbox
{
    "Items": [
        {
            "ID": {
                "S": "001"
            },
            "Point": {
                "S": "999"
            }
        }
    ],
    "Count": 1,
    "ScannedCount": 1,
    "ConsumedCapacity": null
}
$ aws dynamodb scan --table-name Test2 --profile sandbox
{
    "Items": [
        {
            "TotalPoint": {
                "S": "999"
            },
            "ID": {
                "S": "002"
            }
        }
    ],
    "Count": 1,
    "ScannedCount": 1,
    "ConsumedCapacity": null
}

3: テーブルの更新に失敗する場合(テーブル名が不正)

最初のPutコマンドで存在しないテーブル(Test3)を指定してみます。

aws dynamodb transact-write-items --profile sandbox --transact-items '[
    {
        "Put": {
            "TableName" : "Test3",
            "Item" : {
                "ID" : {"S": "001"},
                "Point" : {"S": "5"}
            }
        }
    },
    {
        "Put": {
            "TableName" : "Test2",
            "Item" : {
                "ID" : {"S": "001"},
                "TotalPoint" : {"S": "100"}
            }
        }
    }
]'

以下のエラーが出て失敗しました。

An error occurred (ResourceNotFoundException) when calling the TransactWriteItems operation: Requested resource not found

また、先ほどのコマンドでそれぞれのテーブルを確認したところ、更新されていないことを確認しました。(内容が重複するので割愛)

4: 片方のテーブルの更新に失敗する場合(プライマリキーが不正)

次に、2つ目のPutコマンドで存在しないプライマリキーを(ID_DUMMY)を指定してみます。

aws dynamodb transact-write-items --profile sandbox --transact-items '[
    {
        "Put": {
            "TableName" : "Test1",
            "Item" : {
                "ID" : {"S": "001"},
                "Point" : {"S": "100"}
            }
        }
    },
    {
        "Put": {
            "TableName" : "Test2",
            "Item" : {
                "ID_DUMMY" : {"S": "001"},
                "TotalPoint" : {"S": "200"}
            }
        }
    }
]'

先ほどと同様に、以下エラーが発生しました。

An error occurred (TransactionCanceledException) when calling the TransactWriteItems operation: Transaction cancelled, please refer cancellation reasons for specific reasons [None, ValidationError]

また、先ほどのコマンドでそれぞれのテーブルを確認したところ、更新されていないことを確認しました。(内容が重複するので割愛)

まとめ

DynamoDBのトランザクション処理についてCLIで検証してみました。

割と簡単に実装できそうで、業務でも十分活用できそうなことが分かったので良かったです。

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