kafka-python raise UnrecognizedBrokerVersion Error
Asked Answered
K

2

17

I am getting this error when constructing KafkaProducer with the kafka-python package:

[ERROR] UnrecognizedBrokerVersion: UnrecognizedBrokerVersion
Traceback (most recent call last):
  File "/var/lang/lib/python3.7/imp.py", line 234, in load_module
    return load_source(name, filename, file)
  File "/var/lang/lib/python3.7/imp.py", line 171, in load_source
    module = _load(spec)
  File "<frozen importlib._bootstrap>", line 696, in _load
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/var/task/kafka/producer/kafka.py", line 381, in __init__
    **self.config)
  File "/var/task/kafka/client_async.py", line 240, in __init__
    self.config['api_version'] = self.check_version(timeout=check_timeout)
  File "/var/task/kafka/client_async.py", line 908, in check_version
    version = conn.check_version(timeout=remaining, strict=strict, topics=list(self.config['bootstrap_topics_filter']))
  File "/var/task/kafka/conn.py", line 1228, in check_version
    raise Errors.UnrecognizedBrokerVersion()

The code is as follows:

from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers=os.environ.get('KAFKA_HOST', 'localhost:9092'))

I am using Python 3.7 and an AWS MSK cluster.

Kalevala answered 31/10, 2019 at 9:15 Comment(2)
Looks like the same error in this issue: github.com/dpkp/kafka-python/issues/1796Jalapa
@Jalapa I am not using SSL right now. The last comment said to deactivate SSL. Do you have suggestions?Kalevala
K
28

Solved it by just adding security_protocol="SSL" to the KafkaProducer as follows:

from kafka import KafkaProducer
producer = KafkaProducer(security_protocol="SSL", bootstrap_servers=os.environ.get('KAFKA_HOST', 'localhost:9092'))
Kalevala answered 31/10, 2019 at 9:49 Comment(2)
if don't use SSL can I use PLAINTEXT? import os from kafka import KafkaConsumer producer = KafkaConsumer(security_protocol="PLAINTEXT", bootstrap_servers=os.environ.get('KAFKA_HOST', 'localhost:2181')) after returned error raise Errors.UnrecognizedBrokerVersion() kafka.errors.UnrecognizedBrokerVersion: UnrecognizedBrokerVersion, how solve?Compliancy
@NikolayBaranenko Change your port from 2181 to 9092.Owen
R
4

There are lots of optional parameters with the kafka-python lib methods. Here's a recent working script with which I was able to listen to an Azure server:

import os, kafka  # pip install kafka-python

consumer = kafka.KafkaConsumer("test-topic",
    bootstrap_servers=["test-server.servicebus.windows.net:9093"],
    auto_offset_reset="earliest",
    enable_auto_commit=True,
    group_id="$Default",
    sasl_mechanism="PLAIN",
    sasl_plain_password=os.environ["KAFKA_PASSWORD"],
    sasl_plain_username="$ConnectionString",
    security_protocol="SASL_SSL",
    value_deserializer=lambda x: x.decode("utf-8"))

print(datetime.datetime.now())
for message in consumer:
    message_dict = json.loads(message.value)
    print(datetime.datetime.now())
    print("%s" % (json.dumps(message_dict,indent=4)))

It is crude, but effective. Simpler than the Java-wrapped-in-bash Kafka demo consumer from the https://kafka.apache.org/quickstart page (which required assorted config files and environment variables.)

Remittee answered 8/2, 2021 at 23:45 Comment(1)
what is $ConnectionString, and Password. which key label to use??Inimitable

© 2022 - 2025 — McMap. All rights reserved.