运行生产者或使用者时发生Python Kafka SyntaxError

时间:2019-03-07 04:57:21

标签: python apache-kafka apache-zookeeper

我是kafka的新手,并尝试使用链接tutorial创建一个简单的hello world应用程序。我先执行了zookeeper,然后执行kafka-server-start命令,但是执行生产者时出现以下错误

Traceback (most recent call last):
  File "D:/Python/consumer.py", line 1, in <module>
    from kafka import KafkaConsumer
  File "D:\Python\venv\lib\site-packages\kafka\__init__.py", line 23, in <module>
    from kafka.producer import KafkaProducer
  File "D:\Python\venv\lib\site-packages\kafka\producer\__init__.py", line 4, in <module>
    from .simple import SimpleProducer
  File "D:\Python\venv\lib\site-packages\kafka\producer\simple.py", line 54
    return '<SimpleProducer batch=%s>' % self.async
                                                  ^
SyntaxError: invalid syntax

我尝试将kafka-python版本修改为1.4.3,似乎没有任何效果。任何指针将不胜感激 以下是我的python文件: Producer.py:

from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers='localhost:9092')
producer.send('sample', b'Hello, World!')
producer.send('sample', key=b'message-two', value=b'This is Kafka-Python')

Consumer.py:

from kafka import KafkaConsumer
consumer = KafkaConsumer('sample')
for message in consumer:
    print (message)

我遵循的步骤是:
1.使用命令启动Zookeeper

.\bin\windows\zookeeper-server-start.bat .\config\zookeeper.properties

2。 Zookeeper启动后,我执行了命令

.\bin\windows\kafka-server-start.bat .\config\server.properties

3。启动服务器后,我运行了python producer.py命令,接着运行了Consumer.py

有人可以告诉我我要去哪里错吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

您需要更改python版本3.6。 3.7存在一些兼容性问题。

选中此a python script fails to import kafka library while running inside docker