In this section, we will guide you through the process of setting up Apache Kafka on your local machine. By the end of this module, you will have a working Kafka environment that you can use to practice and experiment with Kafka concepts.
Prerequisites
Before we begin, ensure you have the following prerequisites:
- Java Development Kit (JDK) 8 or higher installed.
- Apache Kafka binary distribution downloaded.
- Basic understanding of command-line operations.
Step-by-Step Guide
- Install Java Development Kit (JDK)
Kafka requires Java to run. Follow these steps to install the JDK:
For Windows:
- Download the JDK from the Oracle website.
- Run the installer and follow the on-screen instructions.
- Set the
JAVA_HOME
environment variable:- Open the Control Panel and go to System and Security > System > Advanced system settings.
- Click on Environment Variables.
- Under System variables, click New and add
JAVA_HOME
with the path to your JDK installation (e.g.,C:\Program Files\Java\jdk-11.0.10
).
For macOS:
- Install Homebrew if you haven't already:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Install the JDK using Homebrew:
brew install openjdk@11
- Add the JDK to your PATH:
echo 'export PATH="/usr/local/opt/openjdk@11/bin:$PATH"' >> ~/.zshrc source ~/.zshrc
For Linux:
- Install the JDK using your package manager. For example, on Ubuntu:
sudo apt update sudo apt install openjdk-11-jdk
- Download and Extract Kafka
- Download the latest Kafka binary distribution from the Apache Kafka website.
- Extract the downloaded archive to a directory of your choice. For example:
tar -xzf kafka_2.13-2.8.0.tgz cd kafka_2.13-2.8.0
- Start the ZooKeeper Server
Kafka uses ZooKeeper to manage distributed brokers. Start the ZooKeeper server using the following command:
You should see logs indicating that the ZooKeeper server has started successfully.
- Start the Kafka Server
In a new terminal window, navigate to the Kafka directory and start the Kafka server:
You should see logs indicating that the Kafka server has started successfully.
- Create a Kafka Topic
Kafka topics are used to organize and store messages. Create a new topic named test
:
bin/kafka-topics.sh --create --topic test --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
You should see a confirmation message indicating that the topic has been created.
- Produce Messages to the Topic
Open a new terminal window and start a Kafka producer to send messages to the test
topic:
Type a few messages and press Enter after each message. The messages will be sent to the Kafka topic.
- Consume Messages from the Topic
Open another terminal window and start a Kafka consumer to read messages from the test
topic:
You should see the messages you produced in the previous step.
Summary
In this section, you have successfully set up a local Kafka environment. You installed the necessary prerequisites, started the ZooKeeper and Kafka servers, created a Kafka topic, and produced and consumed messages. This setup will serve as the foundation for exploring Kafka's core concepts and operations in the subsequent modules.
Next, we will dive into Kafka's core concepts, starting with producers and consumers.
Kafka Course
Module 1: Introduction to Kafka
Module 2: Kafka Core Concepts
Module 3: Kafka Operations
Module 4: Kafka Configuration and Management
Module 5: Advanced Kafka Topics
- Kafka Performance Tuning
- Kafka in a Multi-Data Center Setup
- Kafka with Schema Registry
- Kafka Streams Advanced