-
Notifications
You must be signed in to change notification settings - Fork 0
/
kafka_producer.py
executable file
·49 lines (42 loc) · 1.48 KB
/
kafka_producer.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
#!/usr/bin/env python
import kafka
import logging
from aiven import Producer, Config
import sys
import time
def main():
cfg = Config("config.yml")
p = Producer(
cfg.yaml["kafka"]["host"],
cfg.yaml["kafka"]["topic"],
cfg.yaml["kafka"]["ssl_cafile"],
cfg.yaml["kafka"]["ssl_certfile"],
cfg.yaml["kafka"]["ssl_keyfile"],)
try:
p.open_conn()
except kafka.errors.NoBrokersAvailable as e:
log.fatal("Unable to connect to Kafka! Error: {0}".format(e))
sys.exit(1)
log.info("Ready to produce to topic '{0}'".format(cfg.yaml["kafka"]["topic"]))
counter = 0
# Start sending a message every second, indefinitely
while True:
try:
log.info("Sending 'foo':'{0}' to '{1}'".format(
counter,
cfg.yaml["kafka"]["topic"]))
result = p.producer.send(cfg.yaml["kafka"]["topic"]
, bytes('{"foo":"%d"}' % counter, "utf-8"))
result.get(timeout=5) # Check to make sure kafka is still running
counter+=1
time.sleep(1)
except kafka.errors.KafkaTimeoutError as e:
log.fatal("Can not send message! Error: {0}".format(e)) #Kafka down
sys.exit(1)
if __name__ == "__main__":
logging.basicConfig(
format='[%(asctime)s] %(levelname)s [%(name)s %(process)d] %(message)s',
level=logging.INFO
)
log = logging.getLogger('aiven_producer')
main()