网络编程
位置:首页>> 网络编程>> Python编程>> Python测试Kafka集群(pykafka)实例

Python测试Kafka集群(pykafka)实例

作者:右介  发布时间:2023-05-05 20:57:56 

标签: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

来源:https://www.cnblogs.com/zhangtianyuan/p/7832373.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com