CatTail
8/4/2018 - 7:34 AM

Simple CLI script read kinesis record and log to console

Simple CLI script read kinesis record and log to console

#! /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)