-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKinesisConsumer.py
57 lines (46 loc) · 1.83 KB
/
KinesisConsumer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import boto3
def process_records(Records):
#---------------------------
# Your processing goes here
#---------------------------
print(Records)
def main():
stream_name = 'terraform-kin-stream'
try:
session = boto3.Session(profile_name='tylerw')
kinesis_client = session.client('kinesis')
#------------------
# Get the shard ID.
#------------------
response = kinesis_client.describe_stream(StreamName=stream_name)
shard_id = response['StreamDescription']['Shards'][0]['ShardId']
#---------------------------------------------------------------------------------------------
# Get the shard iterator.
#---------------------------------------------------------------------------------------------
response = kinesis_client.get_shard_iterator(
StreamName=stream_name,
ShardId=shard_id,
ShardIteratorType='LATEST'
)
shard_iterator = response['ShardIterator']
#-----------------------------------------------------------------
# Get the records.
# Get max_records from the shard, or run continuously if you wish.
#-----------------------------------------------------------------
max_records = 100
record_count = 0
while record_count < max_records:
response = kinesis_client.get_records(
ShardIterator=shard_iterator,
Limit=10
)
shard_iterator = response['NextShardIterator']
records = response['Records']
record_count += len(records)
process_records(records)
except ClientError as e:
logger.exception("Couldn't get records from stream %s.", stream_name)
raise
if __name__ == "__main__":
while True:
main()