Table of Contents
- Prerequisites
- 1. Create a Kafka Topic
- 2. Produce Messages with Python
- 3. Consume Messages with Python
- 4. Key Concepts
- References
Prerequisites
- A running Kafka broker on
localhost:9092 - Python 3.6+ installed
- Basic familiarity with the command line
Make sure your Kafka server is up and running. If you haven’t installed Kafka yet, follow the Apache Kafka Quickstart.
1. Create a Kafka Topic
Create a topic namedmulti-partition-topic with 3 partitions and a replication factor of 1:
2. Produce Messages with Python
2.1 Set Up Python Environment
-
Update package lists and install the virtual environment tools:
-
Create and activate a virtual environment:
-
Install the Kafka client library:
2.2 Write the Producer Script
Create a file namedkafka_producer_example.py:
3. Consume Messages with Python
Create a file namedkafka_consumer.py:
4. Key Concepts
| Concept | Description |
|---|---|
| Consumer Group | group_id lets multiple consumers share partition consumption. |
| Offset | Position in a partition. earliest resets the offset to the oldest record. |
| Partition Distribution | Messages with different keys go to different partitions for parallelism. |