Overview
confluent-kafka 2.0, released on January 15, 2023, cleans up the API and drops Python 3.6 support. The library now requires Python 3.7+.
Main Features
API cleanup
Old deprecated methods have been removed and the configuration API has been simplified. Callbacks now use a more Pythonic style.
python
from confluent_kafka import Producer
conf = {
'bootstrap.servers': 'localhost:9092',
'client.id': 'my-producer',
}
producer = Producer(conf)
producer.produce(
topic='events',
key='key-1',
value='test message',
callback=lambda err, msg: print(f'Delivered: {msg.topic()}')
)
producer.flush()
Python 3.7+ required
Dropping Python 3.6 enables the use of dataclasses, postponed annotations, and Python 3.7+ runtime performance improvements.
python
from confluent_kafka import Consumer
consumer = Consumer({
'bootstrap.servers': 'localhost:9092',
'group.id': 'my-group',
'auto.offset.reset': 'earliest',
})
consumer.subscribe(['events'])
while True:
msg = consumer.poll(timeout=1.0)
if msg is None:
continue
if msg.error():
print(f'Error: {msg.error()}')
else:
print(f'Received: {msg.value().decode()}')
break
consumer.close()
