Introduction to Confluent Kafka Python Producer


Today, data is an essential component of the digital ecosystem, and each modern application depends on its effective administration and processing. For this data-driven era, Apache Kafka, a powerful event-streaming technology, provides a high-throughput solution. These potent features are seamlessly integrated into your Python applications using Confluent's Python client for Apache Kafka. This article gives a thorough overview of the Confluent Kafka Python Producer and includes useful examples to get you started.

What is Confluent Kafka Python Producer?

A component of Confluent's Kafka Python client library, the Confluent Kafka Python Producer offers a Pythonic interface to Apache Kafka's powerful data streaming capabilities. In conjunction with the Kafka Consumer, it enables Python programmes to fully participate in Kafka-based distributed systems by enabling them to produce data to Kafka topics.

Getting Started with Confluent Kafka Python Producer

Pip, the package installer for Python, can be used to install the Confluent Kafka Python Producer. To install, issue the following command −

pip install confluent-kafka

You can import the Kafka Producer in your Python script after it has been installed 

from confluent_kafka import Producer

Putting Confluent Kafka Python Producer to Work

Let's now explore how to send messages to Kafka using the Confluent Kafka Python Producer.

Example 1: Producing a Simple Message

How to create a direct response to a Kafka topic is as follows −

from confluent_kafka import Producer

p = Producer({'bootstrap.servers': 'localhost:9092'})

p.produce('mytopic', 'Hello, Kafka!')

p.flush()

This script establishes a connection to a Kafka broker at localhost:9092 by creating a Kafka Producer. To make sure the message has been sent, it first produces the message "Hello, Kafka!" to the subject "mytopic" before flushing the producer's message queue.

Example 2: Handling Message Delivery Reports

Additionally, the Confluent Kafka Producer can report on the success of message delivery to their topic −

from confluent_kafka import Producer

def delivery_report(err, msg):
   if err is not None:
      print(f'Message delivery failed: {err}')
   else:
      print(f'Message delivered to {msg.topic()} [{msg.partition()}]')

p = Producer({'bootstrap.servers': 'localhost:9092'})

p.produce('mytopic', 'Hello, Kafka!', callback=delivery_report)

p.flush()

Here, the message is given when the callback function delivery_report is invoked, which is part of the produce method.

Example 3: Producing Key-Value Messages

Kafka messages frequently contain both a key and a value. How to create a key-value message is as follows 

from confluent_kafka import Producer

p = Producer({'bootstrap.servers': 'localhost:9092'})

p.produce('mytopic', key='mykey', value='myvalue')

p.flush()

This script generates a message for the topic "mytopic" with the keys "mykey" and "myvalue."

Example 4: Producing Avro Messages

With the help of the data serialisation technology Avro, you can encrypt the message's schema. This is particularly helpful when creating communications for a subject that will be consumed by various consumers, each of whom may require a different format. To create Avro messages, follow these steps 

from confluent_kafka import avro, Producer
from confluent_kafka.avro import AvroProducer

value_schema = avro.load('value_schema.avsc')
key_schema = avro.load('key_schema.avsc')
value = {"name": "Value"}
key = {"name": "Key"}

avroProducer = AvroProducer({
   'bootstrap.servers': 'localhost:9092', 
   'schema.registry.url': 'http://127.0.0.1:8081'
   }, default_key_schema=key_schema, default_value_schema=value_schema)

avroProducer.produce(topic='my_topic', value=value, key=key)
avroProducer.flush()

This script creates a message for the topic "my_topic" with a key and value that adheres to the supplied Avro schemas.

Example 5: Configuring Message Compression

To conserve bandwidth, you can set the Kafka Producer to compress messages before sending them. Here's an illustration 

from confluent_kafka import Producer

p = Producer({
   'bootstrap.servers': 'localhost:9092',
   'compression.type': 'gzip',
})

p.produce('mytopic', 'Hello, Kafka!')
p.flush()

This script creates a Kafka Producer that uses gzip to compress messages before delivering them to the topic.

Conclusion

The Kafka Python Producer from Confluent is a powerful and highly adaptable solution that enables Python applications to take advantage of Kafka's strong data streaming features. It's a crucial tool whether you're building a complex distributed system or just need a reliable data stream.

Everything from installation to actual usage in your Python application has been addressed in this thorough analysis. Five examples have been covered in detail: constructing a simple message, managing delivery reports, producing key-value messages, building Avro messages, and customising message compression.

But keep in mind that Confluent's Kafka Python Producer offers much more than what is covered in this book. We advise consulting the official Confluent documentation and carrying on with experimentation for advanced usage, such as integrating with Kafka Streams or developing custom serializers.

Updated on: 17-Jul-2023

952 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements