Kafka Producer Client Python Code not working

1.5k Views Asked by At

I have my zookeeper and kafka cluster all set up and running in my localhost. I have a producer and a consumer running form the command prompt as well which runs fine. Now when I am trying out by using kafka-python library on a python script, I get the following error.

kafka-producer.py

from kafka import KafkaProducer

from kafka.errors import KafkaError

producer = KafkaProducer(bootstrap_servers=['localhost:9092'])


try:
    future = producer.send('topic', b'From program')
    record_metadata = future.get(timeout=60)
    producer.flush()
except KafkaError as exc:
    print("Exception during getting assigned partitions - {}".format(exc))
    # Decide what to do if produce request failed...
    pass

Error:

Exception during getting assigned partitions - KafkaTimeoutError: Batch for TopicPartition(topic='topic', partition=0) containing 1 record(s) expired: 30 seconds have passed since batch creation plus linger time

This topic exists, I have verified that. Even my kafka-consumer.py works fine as I checked it form a producer from command prompt via kafka-console-producer.bat --broker-list localhost:9092 --topic topic.

kafka-consumer.py

from kafka import KafkaConsumer

consumer = KafkaConsumer('topic',auto_offset_reset='earliest',group_id=None,bootstrap_servers=['localhost:9092'])
for msg in consumer:
    print (msg)
0

There are 0 best solutions below