How to run Kafka Server with Producer and Consumer in Windows - Six steps Practical guide

In this post, we will learn that how to install and use Kafka server along with producer and consumer clients message exchange. This guide is based on practical only (no theory). For theory you can see other resources. This is the step by step practical guide that how to run Kafka on windows environment.

Step 1: Download and Extract Kafka in your desired directory:

1. Go to the link Apache Kakfa download and download. (I have download binary version 2.13)
2. Extract the .tgz file into any suitable location.




I have extracted my Kafka installation into C drive insdie Apachekafka Folder. 


Step 2: Install Java run-time and Save its path in environment variable.

1. Go to the link and install java runtime
2. After installation, open Environment variable from my computer properties and add JAVA_HOME variable and provide your java runtime installation path.

Java runtime installation path provided in a new environment variable named JAVA_HOME. 
3. In environment variable, select path, click on edit button and add this string  “;%JAVA_HOME%\bin”
4. Go to command line and test "java -version". If you are not getting java installed version then see

How to solve "npm not recognized as internal or external command"


Step 3: Run Apache Zookeeper and Kafka Server

1. Make a batch file next to your Kafka folder. This batch file will run Zookeeper server. Add below line and run the batch file

start kafka_2.13-2.4.1\bin\windows\zookeeper-server-start.bat kafka_2.13-2.4.1\config\zookeeper.properties

Note:In above batch file command, kafka_2.13-2.4.1 is my Kafka installation folder name put your folder name in both location.

2. Make a batch file for running the Kafka Server and Run the batch file

start kafka_2.13-2.4.1\bin\windows\kafka-server-start.bat kafka_2.13-2.4.1\config\server.properties

Note:In above batch file command, kafka_2.13-2.4.1 is my Kafka installation folder name put your folder name in both location.

Two batch files have created in order to run Zookeeper and Kafka Server. Whenever you want to run Kafka server, you first need to start zookeeper server.

Step 4: Create A Topic

In order to send message from Producer to consumer, you first need to create a topic. Later both producer and consumer will subscribe the topic and send and receive messages.

1. Go to Kafka installation directory, then Bin\Windows, and open command prompt form here and type below lines. You can write your desired name instead of YourTopicName
kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic YourTopicName
A topic with name "YourTopicName" has created successfully. 

Step 5: Start Producer

Kafka provides you message producer that will produce message for a specific topic.
1. Go to kafka installation directory, then Bin\Windows, and open command prompt form here and type below lines. 
kafka-console-producer.bat --broker-list localhost:9092 --topic TopicToSubscribe
Note: TopicToSubscribe is the topic name where you want to send messages, in our case it should be YourTopicName
2. Hit enter to start the producer

Step 6: Start Consumer

As name suggest consumer will get the messages from producer. 
1. Go to kafka installation directory, then Bin\Windows, and open command prompt form here and type below lines. 
kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic TopicToSubscribe
Note: TopicToSubscribe is the topic name from where you want to gete messages , in our case it should be YourTopicName
2. Hit enter to run the consumer
3. Now go to Producer command line, and type any message and enter
4. View the Consumer command line you will get the message.


Post a Comment

0 Comments