nad
nad

Reputation: 2870

KafkaUnavailableError in python app

I am new to Kafka and following this tutorial to go through a simple python app. When I run the producer.py, I get below error

raise KafkaUnavailableError('All servers failed to process request: %s' % hosts)
kafka.errors.KafkaUnavailableError: KafkaUnavailableError: All servers failed to process request: [('localhost', 9092, 0)]

Not sure what am I missing? I made sure I start the kafka service by

brew services start kafka

Any suggestion? My producer.py below;

import time
import cv2
from kafka import SimpleProducer, KafkaClient
#  connect to Kafka
kafka = KafkaClient('localhost:9092')
producer = SimpleProducer(kafka)
# Assign a topic
topic = 'my-topic'

def video_emitter(video):
    # Open the video
    video = cv2.VideoCapture(video)
    print(' emitting.....')

    # read the file
    while (video.isOpened):
        # read the image in each frame
        success, image = video.read()
        # check if the file has read to the end
        if not success:
            break
        # convert the image png
        ret, jpeg = cv2.imencode('.png', image)
        # Convert the image to bytes and send to kafka
        producer.send_messages(topic, jpeg.tobytes())
        # To reduce CPU usage create sleep time of 0.2sec  
        time.sleep(0.2)
    # clear the capture
    video.release()
    print('done emitting')

if __name__ == '__main__':
    video_emitter('video.mp4')

Upvotes: 0

Views: 735

Answers (1)

Giorgos Myrianthous
Giorgos Myrianthous

Reputation: 39950

It looks like you are using the old consumer API which is deprecated. Instead of SimpleProducer you could try using KafkaProducer:

from kafka import KafkaProducer

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

Upvotes: 2

Related Questions