Skip to content

Instantly share code, notes, and snippets.

@torian
Created April 27, 2016 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torian/d1d3e9c3b7487d4b3655168cac3a1776 to your computer and use it in GitHub Desktop.
Save torian/d1d3e9c3b7487d4b3655168cac3a1776 to your computer and use it in GitHub Desktop.
AWS Kinesis shard split
import time
from boto import kinesis
AWS_PROFILE = None
AWS_REGION = 'us-east-1'
AWS_STREAM_NAME = 'YourStreamName'
WAIT_STATUS_SEC = 5
WAIT_STATUS_ROUNDS = 5
options_split = True
options_shard_num = 14
k = kinesis.connect_to_region(AWS_REGION, profile_name = AWS_PROFILE)
stream_info = k.describe_stream(AWS_STREAM_NAME)
shards = {}
shards_num = len(stream_info['StreamDescription']['Shards'])
for shard in stream_info['StreamDescription']['Shards']:
s_id = shard['ShardId']
shards[s_id] = {
'StartingHashKey': int(shard['HashKeyRange']['StartingHashKey']),
'EndingHashKey': int(shard['HashKeyRange']['EndingHashKey']),
}
if options_split:
status = None
i = 0
while status != 'ACTIVE':
status = k.describe_stream(AWS_STREAM_NAME)['StreamDescription']['StreamStatus']
if i > WAIT_STATUS_ROUND or status == 'ACTIVE':
break
i += 1
time.sleep(WAIT_STATUS_SEC)
new_hkey = int((shards[s_id]['StartingHashKey'] + shards[s_id]['EndingHashKey']) / 2)
print "Splitting shardId ", s_id, "- new hash: ", new_hkey
k.split_shard(AWS_STREAM_NAME, s_id, str(new_hkey))
shards_num += 1
if options_shard_num == shards_num:
break
stream_info = k.describe_stream(AWS_STREAM_NAME)
print "Shards after split: ", len(stream_info['StreamDescription']['Shards'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment