今天看看Python客户端。
代码在github上,和以前一样,我们用github上写好的代码进行测试
https://github.com/mumrah/kafka-python
pip install git+git://github.com/mumrah/kafka-python
先确保机器上已经安装了python
输入命令:python
[root@localhost kafka-python]# python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
先来使用Producer
>>> from kafka import KafkaClient, SimpleProducer, SimpleConsumer
>>> kafka = KafkaClient("localhost:9092")
producer = SimpleProducer(kafka)
producer.send_messages("test1","Hello world!")
值得注意的是,Python客户端不需要想Java客户端一样连接zookeeper,Python客户端的运行和Java版的是两个project,所以方法不一样。
用kafka自带的consumer console,我们可以看到输出的message
同样,Python客户端也有一些可控的参数,例如是否同步:
producer = SimpleProducer(kafka,async=True)
producer = SimpleProducer(kafka,req_acks=SimpleProducer.ACK_AFTER_LOCAL_WRITE)
还可以获得response,例如
response = producer.send_messages("test1","Hello world!")
response[0].offset
得到offset
再看看Consumer
consumer = SimpleConsumer(kafka,"python","test1")
for msg in consumer:
print(msg)
结果如图:
然后让我们优雅的退出
kafka.close()
exit()
Kafka 的Python客户端介绍完了,enjoy!