0
import json
from kafka import KafkaProducer

folderName =r'D:\\BigDataLocalSetup\\KafkaProject\\'

    producer = KafkaProducer(
    bootstrap_servers="kafka-project-abdullahqwerty70125- 
dee5.e.aivencloud.com:25025",
    security_protocol="SSL",
    ssl_cafile=folderName + "ca.pem",
    ssl_certfile=folderName + "service.cert",
    ssl_keyfile=folderName + "service.key",
    value_serializer=lambda v: json.dumps(v).encode('ascii'),
    key_serializer=lambda v: json.dumps(v).encode('ascii')
)
producer.send("test-topic",
              key={"key": 1},
              value={"message": "hello world"}
             )
producer.flush()

I'm trying to connect to a Kafka broker using SSL in Python. I've set up the paths to my certificate files, but I keep encountering a FileNotFoundError when running the code.

Here’s a description of what I’m doing:

I’m importing the necessary libraries (json and KafkaProducer from the kafka package). I've set the paths to my SSL certificate files (ca.pem, service.cert, and service.key) using a folderName variable, ensuring that the paths are correctly formatted for Windows by using double backslashes (\) and raw strings. I’m using the KafkaProducer class to establish a connection with the Kafka broker, specifying SSL as the security protocol and providing the paths to the SSL certificate files. I attempt to send a message to a test topic using producer.send() and then flush the producer with producer.flush(). Even after correcting the path issues by using double backslashes (\) and raw strings (e.g., r"path"), I still get the following error:

FileNotFoundError: [Errno 2] No such file or directory

I’m running this code on a Windows machine. The error seems to indicate that the SSL certificates are not being found, but I have verified that the files exist at the specified paths.

CODE
screenshot of the full error message

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.