Created
August 4, 2018 07:34
-
-
Save CatTail/8a31a3a97016e6881c7deb942bc5cd60 to your computer and use it in GitHub Desktop.
Simple CLI script read kinesis record and log to console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import sys | |
import boto3 | |
import json | |
from datetime import datetime | |
import time | |
if len(sys.argv) != 3: | |
print "Usage: read-kinesis.py <region name> <stream name>" | |
exit(1) | |
region_name = sys.argv[1] | |
stream_name = sys.argv[2] | |
kinesis_client = boto3.client('kinesis', region_name=region_name) | |
response = kinesis_client.describe_stream(StreamName=stream_name) | |
shards = response['StreamDescription']['Shards'] | |
index = 0 | |
iterators = [None] * len(shards) | |
while True: | |
if iterators[index] is None: | |
shard_id = shards[index]['ShardId'] | |
shard_iterator = kinesis_client.get_shard_iterator(StreamName=stream_name, ShardId=shard_id, ShardIteratorType='LATEST') | |
shard_iterator = shard_iterator['ShardIterator'] | |
iterators[index] = shard_iterator | |
record_response = kinesis_client.get_records(ShardIterator=iterators[index], Limit=2) | |
iterators[index] = record_response['NextShardIterator'] | |
for record in record_response['Records']: | |
print record | |
time.sleep(0.2) | |
index = (index + 1) % len(shards) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment