Producers publish messages to Kafka topics with optional keys for partitioning and delivery callbacks.
Kafka Producers
# Console producer (testing)
kafka-console-producer.sh --broker-list localhost:9092 --topic orders
# Python producer
pip install confluent-kafka
from confluent_kafka import Producer
conf = {'bootstrap.servers': 'localhost:9092'}
producer = Producer(conf)
def delivery_report(err, msg):
if err:
print('Delivery failed:', err)
else:
print('Delivered to:', msg.topic(), msg.partition(), msg.offset())
producer.produce(
'orders',
key='user-123',
value='{"order_id": 456, "total": 99.99}',
callback=delivery_report
)
producer.flush() # wait for all messages to be delivered