Python测试Kafka集群(pykafka)实例
更新时间:2019年12月23日 09:10:37 作者:右介
今天小编就为大家分享一篇Python测试Kafka集群(pykafka)实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
生产者代码:
# -* coding:utf8 *- from pykafka import KafkaClient host = 'IP:9092, IP:9092, IP:9092' client = KafkaClient(hosts = host) print client.topics # 生产者 topicdocu = client.topics['my-topic'] producer = topicdocu.get_producer() for i in range(100): print i producer.produce('test message ' + str(i ** 2)) producer.stop()
消费者代码:
# -* coding:utf8 *- from pykafka import KafkaClient host = 'IP:9092, IP:9092, IP:9092' client = KafkaClient(hosts = host) print client.topics # 消费者 topic = client.topics['my-topic'] consumer = topic.get_simple_consumer(consumer_group='test', auto_commit_enable=True, auto_commit_interval_ms=1, consumer_id='test') for message in consumer: if message is not None: print message.offset, message.value
以上这篇Python测试Kafka集群(pykafka)实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
python GUI库图形界面开发之PyQt5窗口背景与不规则窗口实例
这篇文章主要介绍了python GUI库图形界面开发之PyQt5窗口背景与不规则窗口实例,需要的朋友可以参考下2020-02-02Python 实现简单的shell sed替换功能(实例讲解)
下面小编就为大家带来一篇Python 实现简单的shell sed替换功能(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-09-09
最新评论