3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[JAWS-UG CLI] DynamoDB:#5 テーブル(ハッシュキー + レンジキー)作成、アイテム登録、スキャン、クエリ

Posted at

https://jawsug-cli.doorkeeper.jp/events/20523 でのハンズオン資料です。

AWS CLIを利用して、DynamoDBのテーブル(ハッシュキー + レンジキー)を作成し、アイテムを登録してみます。

ここでは、DynamoDB開発者ガイドのサンプルテーブル「Thread」をCLIで作成してみます。
http://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/GettingStartedCreateTables.html

前提条件

DynamoDBへの権限

  • DynamoDBに対してフル権限があること。

AWS CLIのバージョン

以下のバージョンで動作確認済

  • AWS CLI 1.7.18
コマンド
aws --version
結果(例)
aws-cli/1.7.18 Python/2.7.5 Darwin/13.4.0
  1. 準備
    =======

0.1. リージョンの決定

作成するユーザのデフォルトリージョンを決めます。
(カレントユーザが利用するカレントリージョンも切り変わります。)

コマンド(東京リージョンの場合)
export AWS_DEFAULT_REGION='ap-northeast-1'

0.2. 変数の確認

プロファイルとリージョンが想定のものになっていることを確認します。

変数の確認
aws configure list
結果(例)
            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile      iam_full-prjZ-mbp13              env    AWS_DEFAULT_PROFILE
      access_key     ****************LOAQ shared-credentials-file
      secret_key     ****************I1O1 shared-credentials-file
          region           ap-northeast-1              env    AWS_DEFAULT_REGION
  1. テーブルの設計
    ==============

テーブル名: Thread (ForumName, Subject, ...)

プライマリキーのタイプ: ハッシュ + レンジ

ハッシュ属性の名前と型

  • 属性名: ForumName
  • (型: String)

範囲属性の名前と型

  • 属性名: Subject
  • (型: String)

プロビジョニングされたスループット:

  • 読み込みキャパシティーユニット: 10
  • 書き込みキャパシティーユニット: 5
  1. 事前準備
    ===========

1.1. DynamoDBテーブル名の決定

コマンド(例)
DYNAMO_TABLE_NAME='Thread'

同じ名前のテーブルが無いことを確認します。

コマンド
aws dynamodb describe-table --table-name ${DYNAMO_TABLE_NAME}
結果
      A client error (ResourceNotFoundException) occurred when calling the DescribeTable operation: Requested resource not found: Table: ProductCatalog not found

1.2. スキーマの定義

ハッシュキーの指定

コマンド(例)
DYNAMO_HASH_NAME='ForumName'
DYNAMO_HASH_TYPE='S'

レンジキーの指定

コマンド(例)
DYNAMO_RANGE_NAME='Subject'
DYNAMO_RANGE_TYPE='S'

CLIパラメータの作成

コマンド
DYNAMO_ATTRIBUTE="AttributeName=${DYNAMO_HASH_NAME},AttributeType=${DYNAMO_HASH_TYPE} AttributeName=${DYNAMO_RANGE_NAME},AttributeType=${DYNAMO_RANGE_TYPE}"
DYNAMO_KEY_SCHEMA="AttributeName=${DYNAMO_HASH_NAME},KeyType=HASH AttributeName=${DYNAMO_RANGE_NAME},KeyType=RANGE"

1.3. プロビジョンドスループットの指定

コマンド(例)
DYNAMO_READ_UNIT='10'
DYNAMO_WRITE_UNIT='5'

DYNAMO_PROV_THPUT="ReadCapacityUnits=${DYNAMO_READ_UNIT},WriteCapacityUnits=${DYNAMO_WRITE_UNIT}"
  1. テーブルの作成
    =================

2.1. テーブルの作成

変数の確認
cat << ETX

        DYNAMO_TABLE_NAME: ${DYNAMO_TABLE_NAME}
        DYNAMO_ATTRIBUTE:  ${DYNAMO_ATTRIBUTE}
        DYNAMO_KEY_SCHEMA: ${DYNAMO_KEY_SCHEMA}
        DYNAMO_PROV_THPUT: ${DYNAMO_PROV_THPUT}

ETX
コマンド
aws dynamodb create-table \
        --table-name ${DYNAMO_TABLE_NAME} \
        --attribute-definitions ${DYNAMO_ATTRIBUTE} \
        --key-schema ${DYNAMO_KEY_SCHEMA} \
        --provisioned-throughput ${DYNAMO_PROV_THPUT}
結果
      {
        "TableDescription": {
          "AttributeDefinitions": [
              {
                  "AttributeName": "ForumName",
                  "AttributeType": "S"
              },
              {
                  "AttributeName": "Subject",
                  "AttributeType": "S"
              }
          ],
          "ProvisionedThroughput": {
              "NumberOfDecreasesToday": 0,
              "WriteCapacityUnits": 5,
              "ReadCapacityUnits": 10
          },
          "TableSizeBytes": 0,
          "TableName": "Thread",
          "TableStatus": "CREATING",
          "KeySchema": [
              {
                  "KeyType": "HASH",
                  "AttributeName": "ForumName"
              },
              {
                  "KeyType": "RANGE",
                  "AttributeName": "Subject"
              }
          ],
          "ItemCount": 0,
          "CreationDateTime": 1427678617.154
        }
      }

2.2. 確認(一覧)

コマンド
aws dynamodb list-tables 
結果(例)
      {
        "TableNames": [
          "Thread"
        ]
      }

2.3. 確認(詳細)

コマンド
aws dynamodb describe-table --table-name ${DYNAMO_TABLE_NAME}
結果
      {
        "Table": {
          "AttributeDefinitions": [
              {
                  "AttributeName": "ForumName",
                  "AttributeType": "S"
              },
              {
                  "AttributeName": "Subject",
                  "AttributeType": "S"
              }
          ],
          "ProvisionedThroughput": {
              "NumberOfDecreasesToday": 0,
              "WriteCapacityUnits": 5,
              "ReadCapacityUnits": 10
          },
          "TableSizeBytes": 0,
          "TableName": "Thread",
          "TableStatus": "ACTIVE",
          "KeySchema": [
              {
                  "KeyType": "HASH",
                  "AttributeName": "ForumName"
              },
              {
                  "KeyType": "RANGE",
                  "AttributeName": "Subject"
              }
          ],
          "ItemCount": 0,
          "CreationDateTime": 1427678617.154
        }
      }
  1. アイテムの作成 (put-item)
    ============================

3.1. Thread 1

sample forum1:

ForumName:
"Amazon DynamoDB"

Subject:
"DynamoDB Thread 1"

Message:
"DynamoDB thread 1 message text"

LastPostedBy:
"User A"

LastPostedDateTime:
DateTime.UtcNow.Subtract(new TimeSpan(14, 0, 0, 0))

Views:
0

Replies:
0

Answered:
false

Tags:
{ "index", "primarykey", "table" }

コマンド
DYNAMO_ITEM_STRING='{"ForumName":{"S":"Amazon DynamoDB"},"Subject":{"S":"DynamoDB Thread 1"},"Message":{"S":"DynamoDB thread 1 message text"},"LastPostedBy":{"S":"User A"},"LastPostedDateTime":{"S":"2015-03-20 00:00:01"},"Views":{"N":"0"},"Replies":{"N":"0"},"Answered":{"BOOL":false},"Tags":{"SS":["index","primarykey","table"]}}'
コマンド
echo ${DYNAMO_ITEM_STRING} | json_verify
結果
      JSON is valid
変数の確認
cat << ETX

        DYNAMO_TABLE_NAME:  ${DYNAMO_TABLE_NAME}
        DYNAMO_ITEM_STRING: ${DYNAMO_ITEM_STRING}

ETX
コマンド
 aws dynamodb put-item \
        --table-name ${DYNAMO_TABLE_NAME} \
        --item "${DYNAMO_ITEM_STRING}"
結果
      (戻り値なし)

3.2. Thread 2

sample forum2:

ForumName:
"Amazon DynamoDB"

Subject:
"DynamoDB Thread 2"

Message:
"DynamoDB thread 2 message text"

LastPostedBy:
"User A"

LastPostedDateTime:
DateTime.UtcNow.Subtract(new TimeSpan(21, 0, 0, 0))

Views:
0

Replies:
0

Answered:
false

Tags:
{ "index", "primarykey", "rangekey" }

コマンド
DYNAMO_ITEM_STRING='{"ForumName":{"S":"Amazon DynamoDB"},"Subject":{"S":"DynamoDB Thread 2"},"Message":{"S":"DynamoDB thread 2 message text"},"LastPostedBy":{"S":"User A"},"LastPostedDateTime":{"S":"2015-03-20 00:01:01"},"Views":{"N":"0"},"Replies":{"N":"0"},"Answered":{"BOOL":false},"Tags":{"SS":["index","primarykey","rangekey"]}}'
コマンド
echo ${DYNAMO_ITEM_STRING} | json_verify
結果
      JSON is valid
変数の確認
cat << ETX

        DYNAMO_TABLE_NAME:  ${DYNAMO_TABLE_NAME}
        DYNAMO_ITEM_STRING: ${DYNAMO_ITEM_STRING}

ETX
コマンド
aws dynamodb put-item \
        --table-name ${DYNAMO_TABLE_NAME} \
        --item "${DYNAMO_ITEM_STRING}"
結果
      (戻り値なし)

3.3. Thread 3

sample forum3:

ForumName:
"Amazon S3"

Subject:
"S3 Thread 1"

Message:
"S3 thread 3 message text"

LastPostedBy:
"User A"

LastPostedDateTime:
DateTime.UtcNow.Subtract(new TimeSpan(7, 0, 0, 0))

Views:
0

Replies:
0

Answered:
false

Tags:
{ "largeobjects", "multipart upload" }

コマンド
DYNAMO_ITEM_STRING='{"ForumName":{"S":"Amazon S3"},"Subject":{"S":"S3 Thread 1"},"Message":{"S":"S3 thread 3 message text"},"LastPostedBy":{"S":"User A"},"LastPostedDateTime":{"S":"2015-03-20 00:02:02"},"Views":{"N":"0"},"Replies":{"N":"0"},"Answered":{"BOOL":false},"Tags":{"SS":["largeobjects","multipart upload"]}}'
コマンド
echo ${DYNAMO_ITEM_STRING} | json_verify
結果
      JSON is valid
変数の確認
cat << ETX

        DYNAMO_TABLE_NAME:  ${DYNAMO_TABLE_NAME}
        DYNAMO_ITEM_STRING: ${DYNAMO_ITEM_STRING}

ETX
コマンド
aws dynamodb put-item \
        --table-name ${DYNAMO_TABLE_NAME} \
        --item "${DYNAMO_ITEM_STRING}"
結果
      (戻り値なし)
  1. スキャン
    =======

4.1. アイテム数確認

コマンド
aws dynamodb scan --table-name ${DYNAMO_TABLE_NAME} --query 'Count'
結果
      3

4.2. 全件表示

コマンド
aws dynamodb scan --table-name ${DYNAMO_TABLE_NAME}
結果
      {
        "Count": 3,
        "Items": [
          {
              "Tags": {
                  "SS": [
                      "largeobjects",
                      "multipart upload"
                  ]
              },
              "LastPostedDateTime": {
                  "S": "2015-03-20 00:02:02"
              },
              "Views": {
                  "N": "0"
              },
              "LastPostedBy": {
                  "S": "User A"
              },
              "Answered": {
                  "BOOL": false
              },
              "Replies": {
                  "N": "0"
              },
              "Message": {
                  "S": "S3 thread 3 message text"
              },
              "Subject": {
                  "S": "S3 Thread 1"
              },
              "ForumName": {
                  "S": "Amazon S3"
              }
          },
          {
              "Tags": {
                  "SS": [
                      "index",
                      "primarykey",
                      "table"
                  ]
              },
              "LastPostedDateTime": {
                  "S": "2015-03-20 00:00:01"
              },
              "Views": {
                  "N": "0"
              },
              "LastPostedBy": {
                  "S": "User A"
              },
              "Answered": {
                  "BOOL": false
              },
              "Replies": {
                  "N": "0"
              },
              "Message": {
                  "S": "DynamoDB thread 1 message text"
              },
              "Subject": {
                  "S": "DynamoDB Thread 1"
              },
              "ForumName": {
                  "S": "Amazon DynamoDB"
              }
          },
          {
              "Tags": {
                  "SS": [
                      "index",
                      "primarykey",
                      "rangekey"
                  ]
              },
              "LastPostedDateTime": {
                  "S": "2015-03-20 00:01:01"
              },
              "Views": {
                  "N": "0"
              },
              "LastPostedBy": {
                  "S": "User A"
              },
              "Answered": {
                  "BOOL": false
              },
              "Replies": {
                  "N": "0"
              },
              "Message": {
                  "S": "DynamoDB thread 2 message text"
              },
              "Subject": {
                  "S": "DynamoDB Thread 2"
              },
              "ForumName": {
                  "S": "Amazon DynamoDB"
              }
          }
        ],
        "ScannedCount": 3,
        "ConsumedCapacity": null
      }
  1. クエリ
    ===============

アイテムを以下の条件でクエリしてみます。

  • ハッシュキー"ForumName"の値: "Amazon DynamoDB"と同じ
  • レンジキー"Subject"の値: "DynamoDB Thread"で始まる

5.1. クエリデータの作成

コマンド
pk=ForumName
pt=S
pv="Amazon DynamoDB"
po="EQ"

rk=Subject
rt=S
rv="DynamoDB Thread"
ro="BEGINS_WITH"
コマンド
STR_QUERY="{\"${pk}\":{\"AttributeValueList\":[{\"${pt}\":\"${pv}\"}],\"ComparisonOperator\":\"${po}\"},\"${rk}\":{\"AttributeValueList\":[{\"${rt}\":\"${rv}\"}],\"ComparisonOperator\":\"${ro}\"}}"

echo ${STR_QUERY} | json_verify

5.2. クエリの実行

クエリを実行します。

コマンド
aws dynamodb query \
        --table-name ${DYNAMO_TABLE_NAME} \
        --key-conditions "${STR_QUERY}"
結果(例)
      {
        "Count": 2,
        "Items": [
          {
              "Tags": {
                  "SS": [
                      "index",
                      "primarykey",
                      "table"
                  ]
              },
              "LastPostedDateTime": {
                  "S": "2015-03-20 00:00:01"
              },
              "Views": {
                  "N": "0"
              },
              "LastPostedBy": {
                  "S": "User A"
              },
              "Answered": {
                  "BOOL": false
              },
              "Replies": {
                  "N": "0"
              },
              "Message": {
                  "S": "DynamoDB thread 1 message text"
              },
              "Subject": {
                  "S": "DynamoDB Thread 1"
              },
              "ForumName": {
                  "S": "Amazon DynamoDB"
              }
          },
          {
              "Tags": {
                  "SS": [
                      "index",
                      "primarykey",
                      "rangekey"
                  ]
              },
              "LastPostedDateTime": {
                  "S": "2015-03-20 00:01:01"
              },
              "Views": {
                  "N": "0"
              },
              "LastPostedBy": {
                  "S": "User A"
              },
              "Answered": {
                  "BOOL": false
              },
              "Replies": {
                  "N": "0"
              },
              "Message": {
                  "S": "DynamoDB thread 2 message text"
              },
              "Subject": {
                  "S": "DynamoDB Thread 2"
              },
              "ForumName": {
                  "S": "Amazon DynamoDB"
              }
          }
        ],
        "ScannedCount": 2,
        "ConsumedCapacity": null
      }

2件表示されればOKです。

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?