2017年4月25日

DynamoDBをAWS CLIで操作してみる(アイテム操作)

Share

公式ドキュメント

前提

参考

事前準備

  • 作業用ディレクトリへ移動
    $ cd ~/dynamodbwork
    

テーブル作成

定義用jsonファイル

$ more ./json/musicTableDefinition.json
{
    "TableName": "Music",
    "AttributeDefinitions": [
        {
            "AttributeName": "Artist",
            "AttributeType": "S"
        },
        {
            "AttributeName": "SongTitle",
            "AttributeType": "S"
        }
    ],
    "KeySchema": [
        {
            "KeyType": "HASH",
            "AttributeName": "Artist"
        },
        {
            "KeyType": "RANGE",
            "AttributeName": "SongTitle"
        }
    ],
    "ProvisionedThroughput": {
        "WriteCapacityUnits": 1,
        "ReadCapacityUnits": 1
    }
}

$ aws dynamodb \
create-table \
--endpoint-url http://localhost:8000 \
--cli-input-json file://json/musicTableDefinition.json

アイテム操作

put-item

定義用jsonファイル

$ more ./json/musicItem.json
{
    "Artist": {
        "S": "No One You Know"
    },
    "SongTitle": {
        "S": "Call Me Today"
    },
    "AlbumTitle": {
        "S": "Somewhat Famous"
    }
}

$ aws dynamodb \
put-item \
--table-name Music \
--item file://json/musicItem.json \
--return-consumed-capacity TOTAL \
--endpoint-url http://localhost:8000

get-item

Key用jsonファイル

$ more ./json/musicItemKey.json
{
    "Artist": {
        "S": "No One You Know"
    },
    "SongTitle": {
        "S": "Call Me Today"
    }
}

$ aws dynamodb \
get-item \
--table-name Music \
--key file://json/musicItemKey.json \
--endpoint-url http://localhost:8000

出力結果のjsonファイル

{
    "Item": {
        "Artist": {
            "S": "No One You Know"
        },
        "SongTitle": {
            "S": "Call Me Today"
        },
        "AlbumTitle": {
            "S": "Somewhat Famous"
        }
    }
}

update-item

Key用jsonファイル

$ more ./json/musicItemKey.json
{
    "Artist": {
        "S": "No One You Know"
    },
    "SongTitle": {
        "S": "Call Me Today"
    }
}

更新用jsonファイル

$ more ./json/musicItemUpdateAttribute.json
{
    "AlbumTitle": {
        "Value": {
            "S":"Somewhat Famous4"
        },
        "Action": "PUT"
    }
}

$ aws dynamodb \
update-item \
--table-name Music \
--key file://json/musicItemKey.json \
--attribute-updates file://json/musicItemUpdateAttribute.json \
--endpoint-url http://localhost:8000

更新後のjsonファイル

{
    "Item": {
        "Artist": {
            "S": "No One You Know"
        },
        "SongTitle": {
            "S": "Call Me Today"
        },
        "AlbumTitle": {
            "S": "Somewhat Famous4"
        }
    }
}

delete-item

Key用jsonファイル

$ more ./json/musicItemKey.json
{
    "Artist": {
        "S": "No One You Know"
    },
    "SongTitle": {
        "S": "Call Me Today"
    }
}

$ aws dynamodb \
delete-item \
--table-name Music \
--key file://json/musicItemKey.json \
--endpoint-url http://localhost:8000

batch-write-item

追加用jsonファイル

$ more ./json/musicItems.json
{
    "Music": [{
            "PutRequest": {
                "Item": {
                    "Artist": {
                        "S": "No One You Know"
                    },
                    "SongTitle": {
                        "S": "Call Me Today"
                    },
                    "AlbumTitle": {
                        "S": "Somewhat Famous"
                    }
                }
            }
        },
        {
            "PutRequest": {
                "Item": {
                    "Artist": {
                        "S": "Acme Band"
                    },
                    "SongTitle": {
                        "S": "Happy Day"
                    },
                    "AlbumTitle": {
                        "S": "Songs About Life"
                    }
                }
            }
        },
        {
            "PutRequest": {
                "Item": {
                    "Artist": {
                        "S": "No One You Know"
                    },
                    "SongTitle": {
                        "S": "Scared of My Shadow"
                    },
                    "AlbumTitle": {
                        "S": "Blue Sky Blues"
                    }
                }
            }
        }
    ]
}

$ aws dynamodb \
batch-write-item \
--request-items file://json/musicItems.json \
--endpoint-url http://localhost:8000

batch-get-item

$ aws dynamodb \
batch-get-item \
--request-items file://json/musicItemKeys.json \
--endpoint-url http://localhost:8000

取得

{
    "UnprocessedKeys": {},
    "Responses": {
        "Music": [
            {
                "AlbumTitle": {
                    "S": "Songs About Life"
                }
            },
            {
                "AlbumTitle": {
                    "S": "Somewhat Famous"
                }
            },
            {
                "AlbumTitle": {
                    "S": "Blue Sky Blues"
                }
            }
        ]
    }
}