0

我刚开始学习 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()
4

1 回答 1

0

哦,我刚刚通过查看您发布的示例意识到字符串必须是字节字符串,前面有一个b字符,如此处所述

此外,您缺少 try-except 子句在服务器未准备好时捕获异常......

于 2016-10-20T02:26:08.837 回答