5
5

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] Kinesis:#5 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
  1. 準備
    ===============================

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}
結果
{
    "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"
                }
            }
        ]
    }
}
  1. 事前準備
    =================

1.1. 利用可能な shardId の確認

"Open" な shardIdを確認します。

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

1.2. マージする shardId の決定

"Open" な状態の隣接する shardId を決めます

command
TARGET_SHARD_ID=shardId-000000000001
MERGED_SHARD_ID=shardId-000000000002
  1. shard のマージ
    =======================

2.1. shard のマージ

shard をマージします

変数の確認
cat << ETX

STREEM : ${STREAM_NAME}
TARGET : ${TARGET_SHARD_ID}
MARGED : ${MERGED_SHARD_ID}

ETX
command
aws kinesis merge-shards \
	--stream-name ${STREAM_NAME} \
	--shard-to-merge ${TARGET_SHARD_ID} \
	--adjacent-shard-to-merge ${MERGED_SHARD_ID}
result
(戻り値無し)

2.2 ストリームの確認

ストーリームを確認します。"StreamStatus" が "ACTIVE" になっていればマージ完了です。

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": {
                    "EndingSequenceNumber": "49550864092662459711139433817050545721599481607404650498", 
                    "StartingSequenceNumber": "49550864092651309338540168505480986788282674130572017666"
                }
            }, 
            {
                "ShardId": "shardId-000000000001", 
                "HashKeyRange": {
                    "EndingHashKey": "170141183460469231731687303715884105727", 
                    "StartingHashKey": "0"
                }, 
                "ParentShardId": "shardId-000000000000", 
                "SequenceNumberRange": {
                    "EndingSequenceNumber": "49550864601498562906012662037471029548617289644346703890", 
                    "StartingSequenceNumber": "49550864601487412533413396725901470615300436606500995090"
                }
            }, 
            {
                "ShardId": "shardId-000000000002", 
                "HashKeyRange": {
                    "EndingHashKey": "340282366920938463463374607431768211455", 
                    "StartingHashKey": "170141183460469231731687303715884105728"
                }, 
                "ParentShardId": "shardId-000000000000", 
                "SequenceNumberRange": {
                    "EndingSequenceNumber": "49550864601520863651211192660612565266889938005852684322", 
                    "StartingSequenceNumber": "49550864601509713278611927349043006333573084968006975522"
                }
            }, 
            {
                "ShardId": "shardId-000000000003", 
                "HashKeyRange": {
                    "EndingHashKey": "340282366920938463463374607431768211455", 
                    "StartingHashKey": "0"
                }, 
                "ParentShardId": "shardId-000000000001", 
                "AdjacentParentShardId": "shardId-000000000002", 
                "SequenceNumberRange": {
                    "StartingSequenceNumber": "49550865346912121539498005854874389596844711860389281842"
                }
            }
        ]
    }
}

2.3 利用可能な shardId の確認

shardId-3 以外は "Close" となっており "StartingHashKey" が shardId-1 と同じ "0" に戻っていることを確認します。

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 Close Start: 0
shardId-000000000002 Close Start: 170141183460469231731687303715884105728
shardId-000000000003  Open Start: 0

完了

次に、ストリームを削除します。
http://qiita.com/daikumatan/items/c43a87b4234b633e3e6a

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?