LoginSignup
5
6

More than 5 years have passed since last update.

[JAWS-UG CLI] Kinesis:#3 Shard の分割

Last updated at Posted at 2015-06-18

注意事項

Amazon Kinesis に無料枠はありません。今回のハンズオンでは2つの shard を 2時間弱使用するため、利用料金として10円程度発生しますのでご注意ください。ハンズオン後はストリームの削除をお忘れなくお願いします。

Kinesis の料金 (2015年5月4日現在)
http://aws.amazon.com/jp/kinesis/pricing/

  • $0.0195 /h (1shardあたり)
  • $0.0430 /1,000,000 PutRecords

前提条件

Kinesis への権限

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

AWS CLI のバージョン

  • 以下のバージョンで動作確認済
    • AWS CLI 1.7.24
コマンド
aws --version
結果(例)
aws-cli/1.7.25 Python/2.7.6 Darwin/14.3.0

0. 準備

0.1. リージョンの決定

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

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

0.2. 変数の確認

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

変数の確認
aws configure list
結果(例)
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile KinesisFull-handsOn-Kinesis-user-5KVYHRGZMYWG              env    AWS_DEFAULT_PROFILE
access_key     ****************HORA shared-credentials-file    
secret_key     ****************F9zU shared-credentials-file    
    region           ap-northeast-1              env    AWS_DEFAULT_REGION

0.3. ストリームの指定

command
aws kinesis list-streams
command
STREAM_NAME='<ストリーム名>'

0.4. ストリームの確認

command
aws kinesis describe-stream --stream-name ${STREAM_NAME}
result
{
    "StreamDescription": {
        "StreamStatus": "ACTIVE", 
        "StreamName": "handson", 
        "StreamARN": "arn:aws:kinesis:ap-northeast-1:XXXXXXXXXXXX:stream/handson", 
        "Shards": [
            {
                "ShardId": "shardId-000000000000", 
                "HashKeyRange": {
                    "EndingHashKey": "340282366920938463463374607431768211455", 
                    "StartingHashKey": "0"
                }, 
                "SequenceNumberRange": {
                    "StartingSequenceNumber": "49550864092651309338540168505480986788282674130572017666"
                }
            }
        ]
    }
}

1. 事前作業

1.1. 利用可能な shardId の確認

現在のshardの状態を確認します。実行結果の1列目の数字は shardId を示しています。2列目はレコードの入力可能かどうかを表し "Open"なら可能, "Close" なら現在入力不可能の状態です。3列目の"start: "は、"startingHashKey" の意味であり、4列目にその値が記載されています。

command
aws kinesis describe-stream --stream-name ${STREAM_NAME} | jq -r '.StreamDescription | .Shards[] | "\(.ShardId) \(.ParentShardId) \( .AdjacentParentShardId) \(.HashKeyRange | .StartingHashKey)"' | sed s/shardId-//g |awk 'BEGIN{i=0;print}{hashKey[i]=$4;state[i]="Open";if($2 != "null"){state[$2/1]="Close"};if($3 != "null"){state[$3/1]="Close"};i++}END{for(j=0;j<i;j++) printf("shardId-%012d %5s start: %s\n",j,state[j],hashKey[j]);}'

(このコマンドの解説はこちら)

result
shardId-000000000000  Open Start: 0

Open な状態の shardId は shardId-000000000000 であることが分かりました

1.2. 分割する shardId の決定

分割する shardId を決定します。

command
SPLIT_SHARD_ID=shardId-000000000000

1.3. 分割位置 (StartingHashKey) の決定

分割する位置 "StartingHashKey" を設定します。shardId-0 は 1つのため, HashKeyRange は "0 〜 2^128 - 1" です。このちょうど半分の位置である "2^128 / 2 = 2^127" で分割することにします。

command
HASH_KEY=`echo "2^127" | bc` && echo ${HASH_KEY}
result
170141183460469231731687303715884105728

2. shard の分割

2.1. shard の分割

shard を分割します。

変数の確認
cat << ETX

StreamName : ${STREAM_NAME}
ShardToSplit : ${SPLIT_SHARD_ID}
HashKey : ${HASH_KEY}

ETX
command
aws kinesis split-shard --stream-name ${STREAM_NAME} --shard-to-split ${SPLIT_SHARD_ID} --new-starting-hash-key ${HASH_KEY}
result
(戻り値無し)

2.2. ストリームの確認

ストリームの状態を確認します。"StreamStatus" が "ACTIVE" に なっていれば shard の分割完了です。

command
aws kinesis describe-stream --stream-name ${STREAM_NAME}
結果
{
    "StreamDescription": {
        "StreamStatus": "ACTIVE", 
        "StreamName": "handson", 
        "StreamARN": "arn:aws:kinesis:ap-northeast-1:XXXXXXXXXXXX:stream/handson", 
        "Shards": [
            {
                "ShardId": "shardId-000000000000", 
                "HashKeyRange": {
                    "EndingHashKey": "340282366920938463463374607431768211455", 
                    "StartingHashKey": "0"
                }, 
                "SequenceNumberRange": {
                    "EndingSequenceNumber": "49550864092662459711139433817050545721599481607404650498", 
                    "StartingSequenceNumber": "49550864092651309338540168505480986788282674130572017666"
                }
            }, 
            {
                "ShardId": "shardId-000000000001", 
                "HashKeyRange": {
                    "EndingHashKey": "170141183460469231731687303715884105727", 
                    "StartingHashKey": "0"
                }, 
                "ParentShardId": "shardId-000000000000", 
                "SequenceNumberRange": {
                    "StartingSequenceNumber": "49550864601487412533413396725901470615300436606500995090"
                }
            }, 
            {
                "ShardId": "shardId-000000000002", 
                "HashKeyRange": {
                    "EndingHashKey": "340282366920938463463374607431768211455", 
                    "StartingHashKey": "170141183460469231731687303715884105728"
                }, 
                "ParentShardId": "shardId-000000000000", 
                "SequenceNumberRange": {
                    "StartingSequenceNumber": "49550864601509713278611927349043006333573084968006975522"
                }
            }
        ]
    }
}

2.3. 利用可能な shardId の確認

shardId-0 が "Close", shardId-1, shardId-2 が "Open" になっていることを確認します。前述のとおり、実行結果の4列目の数字は "StartingHashKey" の値を示していますが、この値が設定した "${HASH_KEY}" と等しくなっていることを確認します。

command
aws kinesis describe-stream --stream-name ${STREAM_NAME} | jq -r '.StreamDescription | .Shards[] | "\(.ShardId) \(.ParentShardId) \( .AdjacentParentShardId) \(.HashKeyRange | .StartingHashKey)"' | sed s/shardId-//g |awk 'BEGIN{i=0;print}{hashKey[i]=$4;state[i]="Open";if($2 != "null"){state[$2/1]="Close"};if($3 != "null"){state[$3/1]="Close"};i++}END{for(j=0;j<i;j++) printf("shardId-%012d %5s start: %s\n",j,state[j],hashKey[j]);}'

(このコマンドの解説はこちら)

result
shardId-000000000000 Close start: 0
shardId-000000000001  Open start: 0
shardId-000000000002  Open start: 170141183460469231731687303715884105728

完了

次に分割し新しく生成された shard に対して 複数のレコードを一括送信してみます。
http://qiita.com/daikumatan/items/7e42247acd92a4b37c24

5
6
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
5
6