下面我们来介绍下如何通过Python连接Kafka
安装pykafka
pip install pykafka
导入并创建client
from pykafka import KafkaClient
client = KafkaClient(hosts=’myd3:9092,myd4:9092,myd5:9092,myd2:9092′)
获取topic
topic = client.topics[‘my_credit_rongshu_report’]
创建Consumer并消费
balanced_consumer = topic.get_balanced_consumer(
consumer_group='my_group3',
auto_commit_enable=True,
zookeeper_connect='myd4:2181,myd5:2181,myd6:2181'
)
for message in balanced_consumer:
if message is not None:
print message.partition.id, message.offset, message.value
更详细的文档移步:
http://pykafka.readthedocs.io/en/latest/