Code and commands for my YouTube video about Apache Kafka with Python.
This repository covers two practical ways to start working with Kafka:
method_1- Kafka with Docker Composemethod_2- Manual Kafka installation with ZooKeeper
The examples use Python and the kafka-python client.
.
├── method_1
│ ├── consumer.py
│ ├── docker-compose.yml
│ └── producer.py
├── method_2
│ ├── consumer.py
│ └── producer.py
└── requirements.txt
- Python 3.11+
- Docker and Docker Compose for Method 1
- Java 11+ for Method 2
Install Python dependencies:
python -m venv venv
source venv/bin/activate
pip install -r requirements.txtGo to the Docker-based example:
cd method_1Start Kafka and ZooKeeper:
docker compose up -dCheck running containers:
docker compose psCreate a topic:
docker compose exec kafka kafka-topics \
--create \
--topic test-topic \
--bootstrap-server localhost:9092 \
--partitions 3 \
--replication-factor 1List topics:
docker compose exec kafka kafka-topics \
--list \
--bootstrap-server localhost:9092Describe the topic:
docker compose exec kafka kafka-topics \
--describe \
--topic test-topic \
--bootstrap-server localhost:9092Start a console producer:
docker compose exec kafka kafka-console-producer \
--topic test-topic \
--bootstrap-server localhost:9092Start a console consumer:
docker compose exec kafka kafka-console-consumer \
--topic test-topic \
--bootstrap-server localhost:9092 \
--from-beginningRun the Python producer:
python producer.pyRun the Python consumer:
python consumer.pyStop the stack:
docker compose downDownload Kafka:
wget https://downloads.apache.org/kafka/3.9.2/kafka_2.13-3.9.2.tgz
tar -xzf kafka_2.13-3.9.2.tgz
cd kafka_2.13-3.9.2Start ZooKeeper:
bin/zookeeper-server-start.sh config/zookeeper.propertiesOpen a second terminal and start Kafka:
bin/kafka-server-start.sh config/server.propertiesOpen a third terminal and create a topic:
bin/kafka-topics.sh --create \
--topic manual-topic \
--bootstrap-server localhost:9092 \
--partitions 3 \
--replication-factor 1List topics:
bin/kafka-topics.sh --list \
--bootstrap-server localhost:9092Describe the topic:
bin/kafka-topics.sh --describe \
--topic manual-topic \
--bootstrap-server localhost:9092Start a console producer:
bin/kafka-console-producer.sh \
--topic manual-topic \
--bootstrap-server localhost:9092Start a console consumer:
bin/kafka-console-consumer.sh \
--topic manual-topic \
--bootstrap-server localhost:9092 \
--from-beginningGo back to this repository root and run the Python examples:
cd /path/to/kafka_tutorial/method_2
python producer.py
python consumer.pyFrom the Kafka directory:
cp config/server.properties config/server-2.propertiesEdit these values inside config/server-2.properties:
broker.id=1
listeners=PLAINTEXT://:9093
log.dirs=/tmp/kafka-logs-2Start the second broker:
bin/kafka-server-start.sh config/server-2.propertiesCreate a replicated topic:
bin/kafka-topics.sh --create \
--topic replicated-topic \
--bootstrap-server localhost:9092 \
--partitions 4 \
--replication-factor 2Describe it:
bin/kafka-topics.sh --describe \
--topic replicated-topic \
--bootstrap-server localhost:9092If you first send plain text through kafka-console-producer and then read the same topic with a Python consumer that expects JSON, you can hit this error:
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
This repository uses a safer consumer implementation:
- It decodes the message as text first
- It tries to parse JSON
- If parsing fails, it prints the raw message instead of crashing
If you want a clean demo, recreate the topic before running the Python JSON examples.
Delete and recreate a Docker topic:
docker compose exec kafka kafka-topics \
--delete \
--topic test-topic \
--bootstrap-server localhost:9092
docker compose exec kafka kafka-topics \
--create \
--topic test-topic \
--bootstrap-server localhost:9092 \
--partitions 3 \
--replication-factor 1The examples show:
- a basic Kafka producer
- a basic Kafka consumer
- JSON serialization with Python
- partition and offset output for debugging
- safer message deserialization for mixed message formats
- What a message broker is
- Why Kafka is popular
- Topics, partitions, and replication
- Consumer groups and offsets
- Docker-based Kafka setup
- Manual Kafka installation with ZooKeeper
- Python producers and consumers
- Method 1 is the fastest way to start locally.
- Method 2 is useful if you want to understand what happens under the hood.
- A future Part 3 can cover KRaft mode and a more production-oriented Kafka setup.