-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkafkaHelper.py
41 lines (33 loc) · 1.36 KB
/
kafkaHelper.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
import time, json
import numpy as np
import datetime as dt
from kafka import KafkaProducer, KafkaConsumer
from config import config
def initProducer():
# init an instance of KafkaProducer
print('Initializing Kafka producer at {}'.format(dt.datetime.utcnow()))
producer = KafkaProducer(
bootstrap_servers=config['kafka_broker'],
value_serializer=lambda v: json.dumps(v).encode('utf-8')
)
print('Initialized Kafka producer at {}'.format(dt.datetime.utcnow()))
return producer
def initConsumer(topic, timeout=1000):
# init an instance of KafkaConsumer
consumer = KafkaConsumer(topic, bootstrap_servers=config['kafka_broker'], group_id=None,
auto_offset_reset='earliest', enable_auto_commit=False, consumer_timeout_ms=timeout,
value_deserializer=lambda m: json.loads(m.decode('utf-8')))
return consumer
def produceRecord(data, producer, topic, partition=0):
# act as a producer sending records on kafka
producer.send(topic=topic, partition=partition, value=data)
# debug \ message in prompt
# print('Produce record to topic \'{0}\' at time {1}'.format(topic, dt.datetime.utcnow()))
def consumeRecord(consumer):
rec_list = []
# append to list any new records in consumer
for rec in consumer:
r = rec.value
rec_list.append(r)
# return list of new records
return rec_list