公式ドキュメント

前提

参考

事前準備

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

テーブル作成

定義用jsonファイル

1
$ more ./json/musicTableDefinition.json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
    "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
    }
}

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

アイテム操作

put-item

定義用jsonファイル

1
$ more ./json/musicItem.json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
{
    "Artist": {
        "S": "No One You Know"
    },
    "SongTitle": {
        "S": "Call Me Today"
    },
    "AlbumTitle": {
        "S": "Somewhat Famous"
    }
}

1
2
3
4
5
6
$ 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ファイル

1
$ more ./json/musicItemKey.json
1
2
3
4
5
6
7
8
{
    "Artist": {
        "S": "No One You Know"
    },
    "SongTitle": {
        "S": "Call Me Today"
    }
}

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

出力結果のjsonファイル

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{
    "Item": {
        "Artist": {
            "S": "No One You Know"
        },
        "SongTitle": {
            "S": "Call Me Today"
        },
        "AlbumTitle": {
            "S": "Somewhat Famous"
        }
    }
}

update-item

Key用jsonファイル

1
$ more ./json/musicItemKey.json
1
2
3
4
5
6
7
8
{
    "Artist": {
        "S": "No One You Know"
    },
    "SongTitle": {
        "S": "Call Me Today"
    }
}

更新用jsonファイル

1
$ more ./json/musicItemUpdateAttribute.json
1
2
3
4
5
6
7
8
{
    "AlbumTitle": {
        "Value": {
            "S":"Somewhat Famous4"
        },
        "Action": "PUT"
    }
}

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

更新後のjsonファイル

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{
    "Item": {
        "Artist": {
            "S": "No One You Know"
        },
        "SongTitle": {
            "S": "Call Me Today"
        },
        "AlbumTitle": {
            "S": "Somewhat Famous4"
        }
    }
}

delete-item

Key用jsonファイル

1
$ more ./json/musicItemKey.json
1
2
3
4
5
6
7
8
{
    "Artist": {
        "S": "No One You Know"
    },
    "SongTitle": {
        "S": "Call Me Today"
    }
}

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

batch-write-item

追加用jsonファイル

1
$ more ./json/musicItems.json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{
    "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"
                    }
                }
            }
        }
    ]
}

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

batch-get-item

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

取得

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
    "UnprocessedKeys": {},
    "Responses": {
        "Music": [
            {
                "AlbumTitle": {
                    "S": "Songs About Life"
                }
            },
            {
                "AlbumTitle": {
                    "S": "Somewhat Famous"
                }
            },
            {
                "AlbumTitle": {
                    "S": "Blue Sky Blues"
                }
            }
        ]
    }
}