python3生产者kafka报错:TypeError: ("Producer.produce accepts a bytes object as message, but it got '%s'",

由于官方文档https://pypi.org/project/pykafka/2.8.0.dev2/的demo生产者传入消息格式为

>>> with topic.get_sync_producer() as producer:
...     for i in range(4):
...         producer.produce('test message ' + str(i ** 2))

但是实际按此执行时会报错如下:

TypeError: ("Producer.produce accepts a bytes object as message, but it got '%s'", )

原因为Producer.produce只能接收bytes类型的消息

因此将消息转换为bytes类型即可

producer.produce(bytes(self.message, encoding='utf-8'))

#self.message为传入消息内容

 

你可能感兴趣的:(kafka)