kafka producer使用python:typeerror:all produce message有效负载必须为null或类型bytes

dy2hfwbg  于 2021-06-07  发布在  Kafka
关注(0)|答案(1)|浏览(387)

我刚开始学Python和Kafka。这是我尝试开始的第一个例子。http://www.giantflyingsaucer.com/blog/?p=5541
我有个例外:

Traceback (most recent call last):
  File "producer.py", line 23, in <module>
    main()
  File "producer.py", line 18, in main
    print_response(producer.send_messages(topic, msg))
  File "D:\Setups\Python35-32\lib\site-packages\kafka\producer\simple.py", line 50, in send_messages
    topic, partition, *msg
  File "D:\Setups\Python35-32\lib\site-packages\kafka\producer\base.py", line 379, in send_messages
    return self._send_messages(topic, partition, *msg)
  File "D:\Setups\Python35-32\lib\site-packages\kafka\producer\base.py", line 396, in _send_messages
    raise TypeError("all produce message payloads must be null or type bytes")
TypeError: all produce message payloads must be null or type bytes

我在谷歌上搜索过,但我不太确定问题出在哪里。谁能给我一些建议吗?非常感谢你!
这是我的密码:

from kafka import SimpleProducer, KafkaClient 

def print_response(response=None):
    if response:
        print('Error: {0}'.format(response[0].error))
        print('Offset: {0}'.format(response[0].offset))

def main():
    kafka = KafkaClient("10.2.5.53:9092")
    producer = SimpleProducer(kafka)

    topic = 'test'
    msg = 'Hello World'

    print_response(producer.send_messages(topic, msg))

    kafka.close()

if __name__ == "__main__":
    main()
erhoui1w

erhoui1w1#

哦,我刚从你发布的示例中意识到字符串必须是字节字符串,前面有一个 b 角色,如这里所述
此外,您还缺少try except子句来捕获服务器未就绪时的异常。。。

相关问题