pulsar-2-python操作pulsar生产和消费数据

参考pulsar官方文档

1 安装pulsar-client

目前python的pulsar-client工具包仅支持linux。
pulsar-2-python操作pulsar生产和消费数据_第1张图片#pip3 install jupyter notebook
#pip install --upgrade pip
#pip3 install pulsar-client安装 Pulsar 的 Python 客户端库

1.1 生产数据

import pulsar
import time
client = pulsar.Client('pulsar://10.23.241.202:6650')
producer = client.create_producer('my-topic')

for i in range(100):
    print(i)
    producer.send(('hello-pulsar-%d' % i).encode('utf-8'))
    time.sleep(1)

client.close()

1.2 消费数据

import pulsar
import time

client = pulsar.Client('pulsar://10.23.241.202:6650')
consumer = client.subscribe('my-topic',subscription_name='my-sub')

while True:
    msg = consumer.receive()
    print("Received message: '%s'" % msg.data())
    consumer.acknowledge(msg)
    time.sleep(0.1)
client.close()

你可能感兴趣的:(kafka)