我正在学习Kafka,我正在尝试使用python编写自己的制作人。
我试图使用简单的循环,但是我得到了断言错误:
from kafka import KafkaProducer
from kafka.errors import KafkaError
producer = KafkaProducer(bootstrap_servers=['127.0.0.1:7092'])
for x in range (10):
topic = "kafkatopic"
producer.send(topic=topic,value="text")
x=x+1
不确定原因。
答案 0 :(得分:2)
您可以测试此代码并使用Kafka Python client包:
producer.py
:
from kafka import KafkaProducer
producer = KafkaProducer(
bootstrap_servers=['XX.XXX.XXX.XXX:9092'],
)
for x in range (10):
producer.send('kafkatopic', b'this is a test')
producer.flush()
print('finish')
consumer.py
from kafka import KafkaConsumer
consumer = KafkaConsumer('kafkatopic',
bootstrap_servers=['XX.XXX.XXX.XXX:9092'],
auto_offset_reset= 'smallest')
for message in consumer:
print ("%s:%d:%d: key=%s value=%s" % (message.topic, message.partition,message.offset, message.key,message.value))