from kafka import KafkaProducer producer = KafkaProducer(bootstrap_servers=['localhost:9092']) producer.send('test_topic', b'Hello from Python!') producer.flush()
from kafka import KafkaProducer producer = KafkaProducer(bootstrap_servers=['localhost:9092']) producer.send('test_topic', key=b'key1', value=b'Hello from Python with a key!') producer.flush()
from kafka import KafkaProducer producer = KafkaProducer(bootstrap_servers=['localhost:9092']) future = producer.send('test_topic', b'Hello from Python!') result = future.get(timeout=60) print(result)In this example, a message is sent to the test_topic topic, but this time a future object is returned. By calling future.get(), we wait for the acknowledgement from Kafka that the message has been sent. The timeout parameter specifies how long to wait for the acknowledgement, in seconds. Overall, KafkaProducer is a useful package library for Python developers who need to interact with Apache Kafka. It provides an easy-to-use API for sending messages to Kafka topics, with options for using keys and synchronous sends.