-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathredis-publisher.py
63 lines (48 loc) · 1.8 KB
/
redis-publisher.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
58
59
60
61
62
from kafka import KafkaConsumer
import argparse
import atexit
import logging
import redis
# - default kafka topic to write to
topic_name = 'stock-analyzer'
# - default kafka broker location
kafka_broker = '127.0.0.1:9092'
logger_format = '%(asctime)-15s %(message)s'
logging.basicConfig(format=logger_format)
logger = logging.getLogger('redis-publisher')
logger.setLevel(logging.DEBUG)
def shutdown_hook(kafka_consumer):
"""
a shutdown hook to be called before the shutdown
:param kafka_consumer: instance of a kafka consumer
:return: None
"""
logger.info('Shutdown kafka consumer')
kafka_consumer.close()
if __name__ == '__main__':
# - setup command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('topic_name', help='the kafka topic consume from')
parser.add_argument('kafka_broker', help='the location of the kafka broker')
parser.add_argument('redis_channel', help='the redis channel to publish to')
parser.add_argument('redis_host', help='the location of the redis server')
parser.add_argument('redis_port', help='the redis port')
# - parse arguments
args = parser.parse_args()
topic_name = args.topic_name
kafka_broker = args.kafka_broker
redis_channel = args.redis_channel
redis_host = args.redis_host
redis_port = args.redis_port
# - instantiate a simple kafka consumer
kafka_consumer = KafkaConsumer(
topic_name,
bootstrap_servers=kafka_broker
)
# - instantiate a redis client
redis_client = redis.StrictRedis(host=redis_host, port=redis_port)
# - setup proper shutdown hook
atexit.register(shutdown_hook, kafka_consumer)
for msg in kafka_consumer:
logger.info('Received new data from kafka %s' % str(msg))
redis_client.publish(redis_channel, msg.value)