-
Notifications
You must be signed in to change notification settings - Fork 9
Python Kafka Client Setup on MacOSX
Richard Hightower edited this page Jun 29, 2017
·
1 revision
There are many Python libs for Kafka. For now, we are using Confluent Kafka's version. We tested it and it works well.
We picked this one because it is supported by Confluent and it is based on the Kafka C lib so it should be as fast if not faster than the pure Python versions.
Using Lab 10 from the Java Kafka Course. Start up ZooKeeper and the three servers (each in there own terminal).
$ pwd
~/kafka-training/lab10/solution
bin/run-zookeeper.sh
bin/start-1st-server.sh
bin/start-2nd-server.sh
bin/start-3rd-server.sh
From the IDE or the command line run com.cloudurable.kafka.producer.StockPriceProducer
.
Install the C lib, the python lib, and run then run a consumer script described below.
brew install librdkafka #C Kafka Lib
pip install confluent-kafka #Confluent Python Lib
Create a simple script to consum a Stock Price.
from confluent_kafka import Consumer, KafkaError
c = Consumer({'bootstrap.servers': 'localhost:9092,localhost:9093,localhost:9094',
'group.id': 'myPyConsumer'
})
c.subscribe(['stock-prices'])
running = True
while running:
msg = c.poll()
if not msg.error():
print('Received message: %s' % msg.value().decode('utf-8'))
elif msg.error().code() != KafkaError._PARTITION_EOF:
print(msg.error())
running = False
c.close()
Run the script.
python consumer.py