我正在尝试连接到远程Kafka服务器,但我无法传递SimpleClient错误。 Kafka服务器已经运行,并且已经为我的设备创建了Kafka主题。在尝试写入Kafka之前,我是否需要设置其他内容?
from kafka import SimpleProducer, SimpleClient
kafka = SimpleClient(hosts=["XX.XX.XXX.XXX:2181"]) # remote Ip:port
producer = SimpleProducer(kafka)
topic = "testTopic"
producer.send_messages(topic,"Test message")
这是错误:
Traceback (most recent call last):
File "C:/Users/..../kafkaProducer.py", line 6, in <module>
client = SimpleClient(hosts=["xxx:2181"])
File "C:\Python27\lib\site-packages\kafka\client.py", line 57, in __init__
self.load_metadata_for_topics() # bootstrap with all metadata
File "C:\Python27\lib\site-packages\kafka\client.py", line 531, in load_metadata_for_topics
resp = self.send_metadata_request(topics)
File "C:\Python27\lib\site-packages\kafka\client.py", line 596, in send_metadata_request
return self._send_broker_unaware_request(payloads, encoder, decoder)
File "C:\Python27\lib\site-packages\kafka\client.py", line 186, in _send_broker_unaware_request
raise KafkaUnavailableError('All servers failed to process request: %s' % hosts)
kafka.errors.KafkaUnavailableError: KafkaUnavailableError: All servers failed to process request: [('XX.XX.XX.162', 2181, 0)]
更新:Kafka未在服务器上运行,端口为9092。
from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers=["XX.XX.XXX.XXX:9092"]) # remote Ip:port
topic = "testTopic"
producer.send(topic,"Test message")