Kafka Python Examples: Master Real-Time Data Streaming in Minutes

Logan Jul 13, 2026

Kafka, an open-source stream-processing software platform, has gained immense popularity due to its ability to handle real-time data feeds and streams. When it comes to Python, the official librdkafka library allows developers to leverage the power of Kafka right from their Python applications. Let's delve into some practical examples of how you can use Kafka with Python.

Alex Xu (@alexxubyte) on X
Alex Xu (@alexxubyte) on X

The first step in interacting with Kafka using Python is to install the required libraries. You can use pip to install librdkafka and python-kafka, the two most popular Kafka libraries for Python.

(25) Feed | LinkedIn Cloud Computing Services, Charts And Graphs, Cloud Computing, Computer Science, Software Development, Software, Computer, Science, Engineering
(25) Feed | LinkedIn Cloud Computing Services, Charts And Graphs, Cloud Computing, Computer Science, Software Development, Software, Computer, Science, Engineering

Producer - Sending Messages to Kafka

The producer is responsible for sending messages to Kafka topics. In Python, you can create a producer and publish to a topic as follows:

kafka
kafka

First, import the required library:

```python from kafka import KafkaProducer ```

Then, initialize the producer:

the diagram shows how kafka works and what it is like to work with them
the diagram shows how kafka works and what it is like to work with them

```python producer = KafkaProducer(bootstrap_servers='localhost:9092') ```

Finally, send messages:

```python producer.send('my-topic', key='my-key', value='my-value') producer.flush() ```

Asynchronous Producing

Kafka producers can be configured to send messages asynchronously. This can increase throughput by exploiting parallelism within the producer client and the brokers.

a poster describing the different types of pythons and how they use them to learn
a poster describing the different types of pythons and how they use them to learn

To make a producer asynchronous, you can set the property `ackinsamet Bélgicaes` to 'all'. This tells the producer to send batches of records instead of sending each record individually:

```python producer = KafkaProducer(bootstrap_servers='localhost:9092', acks='all') ```

Consumer - Reading Messages from Kafka

Kafka consumers are responsible for reading messages from Kafka topics. In Python, you can set up a consumer using the following steps:

Top 5 Kafka Use Cases
Top 5 Kafka Use Cases

First, install and import the consumer library:

```python from kafka import KafkaConsumer ```

Then, initialize the consumer:

an image of a diagram with words and symbols on it, including the name of each language
an image of a diagram with words and symbols on it, including the name of each language
Rabbit MQ vs Kafka
Rabbit MQ vs Kafka
the book cover for kafka the definitive guide, with an image of a bird on
the book cover for kafka the definitive guide, with an image of a bird on
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
the top 8 kafka use cases
the top 8 kafka use cases
Brij kishore Pandey on LinkedIn: RabbitMQ vs Kafka: Choosing the Right Message Broker for Your Architecture…
Brij kishore Pandey on LinkedIn: RabbitMQ vs Kafka: Choosing the Right Message Broker for Your Architecture…
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
cat x python
cat x python
Alex Xu (@alexxubyte) on X
Alex Xu (@alexxubyte) on X
three different types of python programming
three different types of python programming
the core python syllabus is shown in red and blue, with text on it
the core python syllabus is shown in red and blue, with text on it
Python Cheat Sheet for Beginners
Python Cheat Sheet for Beginners
Python Cheatsheet
Python Cheatsheet
Apache Kafka For Data Pipeline
Apache Kafka For Data Pipeline
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
The Power of Kafka: Top 5 Use Cases Revealed
The Power of Kafka: Top 5 Use Cases Revealed
the diagram shows how to use kafka for data processing and storage in an application
the diagram shows how to use kafka for data processing and storage in an application
Python Learning Path for Beginners 2026 🚀
Python Learning Path for Beginners 2026 🚀
the differences between kafka and buffers are shown in this graphic above it
the differences between kafka and buffers are shown in this graphic above it
Kafka intro
Kafka intro

```python consumer = KafkaConsumer('my-topic', bootstrap_servers='localhost:9092') ```

Now, the consumer will read and print each message on the topic:

```python for message in consumer: print(f"{message.value} with key: {message.key}") ```

Creating a Consumer Group

Often, multiple consumers work together to read from a Kafka topic. These consumers are grouped together as a consumer group. To create a new consumer group, specify the `group_id` parameter when creating the consumer:

```python consumer = KafkaConsumer('my-topic', bootstrap_servers='localhost:9092', group_id='my-group') ```

This allows all consumers in the same group to read and process the same stream of messages together.

In conclusion, Kafka's powerful stream-processing capabilities can be readily harnessed from Python. By using Kafka producers and consumers, developers can send and receive real-time data between systems, enabling real-time analytics, monitoring, and more. Don't just stop at the basic examples - explore further and build real-time applications today!