Skip to content

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kafka Tutorial Repository

Code and commands for my YouTube video about Apache Kafka with Python.

This repository covers two practical ways to start working with Kafka:

  1. method_1 - Kafka with Docker Compose
  2. method_2 - Manual Kafka installation with ZooKeeper

The examples use Python and the kafka-python client.

Repository Structure

.
├── method_1
│   ├── consumer.py
│   ├── docker-compose.yml
│   └── producer.py
├── method_2
│   ├── consumer.py
│   └── producer.py
└── requirements.txt

Prerequisites

  • 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.txt

Method 1: Kafka with Docker Compose

Go to the Docker-based example:

cd method_1

Start Kafka and ZooKeeper:

docker compose up -d

Check running containers:

docker compose ps

Create a topic:

docker compose exec kafka kafka-topics \
  --create \
  --topic test-topic \
  --bootstrap-server localhost:9092 \
  --partitions 3 \
  --replication-factor 1

List topics:

docker compose exec kafka kafka-topics \
  --list \
  --bootstrap-server localhost:9092

Describe the topic:

docker compose exec kafka kafka-topics \
  --describe \
  --topic test-topic \
  --bootstrap-server localhost:9092

Start a console producer:

docker compose exec kafka kafka-console-producer \
  --topic test-topic \
  --bootstrap-server localhost:9092

Start a console consumer:

docker compose exec kafka kafka-console-consumer \
  --topic test-topic \
  --bootstrap-server localhost:9092 \
  --from-beginning

Run the Python producer:

python producer.py

Run the Python consumer:

python consumer.py

Stop the stack:

docker compose down

Method 2: Manual Kafka Installation with ZooKeeper

Download 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.2

Start ZooKeeper:

bin/zookeeper-server-start.sh config/zookeeper.properties

Open a second terminal and start Kafka:

bin/kafka-server-start.sh config/server.properties

Open a third terminal and create a topic:

bin/kafka-topics.sh --create \
  --topic manual-topic \
  --bootstrap-server localhost:9092 \
  --partitions 3 \
  --replication-factor 1

List topics:

bin/kafka-topics.sh --list \
  --bootstrap-server localhost:9092

Describe the topic:

bin/kafka-topics.sh --describe \
  --topic manual-topic \
  --bootstrap-server localhost:9092

Start a console producer:

bin/kafka-console-producer.sh \
  --topic manual-topic \
  --bootstrap-server localhost:9092

Start a console consumer:

bin/kafka-console-consumer.sh \
  --topic manual-topic \
  --bootstrap-server localhost:9092 \
  --from-beginning

Go back to this repository root and run the Python examples:

cd /path/to/kafka_tutorial/method_2
python producer.py
python consumer.py

Running a Second Broker Locally

From the Kafka directory:

cp config/server.properties config/server-2.properties

Edit these values inside config/server-2.properties:

broker.id=1
listeners=PLAINTEXT://:9093
log.dirs=/tmp/kafka-logs-2

Start the second broker:

bin/kafka-server-start.sh config/server-2.properties

Create a replicated topic:

bin/kafka-topics.sh --create \
  --topic replicated-topic \
  --bootstrap-server localhost:9092 \
  --partitions 4 \
  --replication-factor 2

Describe it:

bin/kafka-topics.sh --describe \
  --topic replicated-topic \
  --bootstrap-server localhost:9092

Common Issue: JSONDecodeError

If 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 1

Python Examples

The 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

Video Topics Covered

  • 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

Notes

  • 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.

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages