Setup and Run Apache Kafka on Windows

To get started with Apache Kafka on Windows, complete the instructions given in this quickstart tutorial.

Note: Your computer must have Java 8+ installed.

Download Kafka

  1. Download latest stable Apache Kafka and move it to the C: Drive. After moving the file, extract it there.
  2. Note: Try to keep the length of the path short by not placing the Kafka file in folders with long name. Doing so will cause error with message "The input line is too long. The syntax of the command is incorrect" while running the Kafka server.

  3. Go inside the extracted Kafka folder and update the dataDir in config\zookeeper.properties file as shown in the example below:
  4. 
        //update this 
        dataDir=/tmp/zookeeper 
    
        //to 
        dataDir=C:/kafka/data/zookeeper
       
  5. Update the log.dirs in config\server.properties file (located in the same config directory).
  6. 
        //update this
        /log.dirs=/tmp/kafka-logs 
        
        //to
        log.dirs=C:/kafka/data/kafka-logs
       

Start the Kafka Services

  1. Open terminal, change your directory to kafka_2.13.-2.6.0\bin\windows and start the ZooKeeper service:
  2. 
                C:\Users\my-pc\>cd \ ..
                C:\>cd kafka_2.13-2.6.0\bin\windows
                C:\kafka_2.13-2.6.0\bin\windows>zookeeper-server-start.bat ..\..\config\zookeeper.properties
                
  3. Open new terminal and start the Kafka broker service :
  4. 
                C:\Users\my-pc\>cd \ ..
                C:\>cd kafka_2.13-2.6.0\\bin\\windows
                C:\kafka_2.13-2.6.0\bin\windows>kafka-server-start.bat ..\..\config\server.properties
                

After starting these services, your basic Kafka environment should be ready for use.

Create a Topic

Next, you must create a topic to store events. A topic in Kafka, is like a table in a database where data are stored. You can have multiple topics for different events. To create a new topic, run the following command by replacing my-topic-name with the name of your topic:


        C:\Users\\buddy\>cd \ ..
        C:\>cd kafka_2.13-2.6.0\\bin\\windows
        C:\kafka_2.13-2.6.0\bin\windows>kafka-topics.bat --create --topic my-topic-name --bootstrap-server localhost:9092
        

You can also view details of the new topic by running the following command:


        C:\kafka_2.13-2.6.0\bin\windows>kafka-topics.bat --describe --topic my-topic-event --bootstrap-server localhost:9092
        

Write Events to the Topic

A Kafka client communicates with the Kafka broker to write events. After the events are written, the broker will store the events for as long as you want.

There are Kafka clients libraries, supported for different languages, using which we can send events to Kafka topics. An application that sends data to the Kafka topic is called a producer application.

You can also use the console Producer to write events to the topic. By default, each entered line is treated as a separated event.


        C:\Users\buddy\>cd \ ..
        C:\>cd kafka_2.13-2.6.0\bin\windows
        C:\kafka_2.13-2.6.0\bin\windows>kafka-console-producer.bat --topic my-topic-name --bootstrap-server localhost:9092
        >This is my first event
        >This is my second event
        >This is my third event
    

The console Producer client can be stopped by pressing Crtl+C at any time.

Read Events from the Topic

A Kafka client communicates with the Kafka broker to read events.

You can use Kafka clients libraries in your application to read events from Kafka topics. An application that reads data from the Kafka topics is called a consumer application.

You can also use console Consumer client to read events that you created. Following is the command to read events:


    C:\Users\buddy\>cd \ ..
    C:\>cd kafka_2.13-2.6.0\bin\windows
    C:\kafka_2.13-2.6.0\bin\windows>kafka-console-consumer.bat --topic my-topic-name --from-beginning --bootstrap-server localhost:9092
    

You can stop the Consumer client by pressing Ctrl+C at any time, followed by entering Y (Yes).

Stopping the Kafka Services

  1. Stop the Kafka console Producer and Consumer clients.
  2. Next, stop the Kafka broker by pressing Ctrl+C, followed by entering Y (Yes).
  3. Lastly, stop the Kafka ZooKeeper by pressing Ctrl+C, followed by entering Y (Yes).

Import/Export Data as Streams of Events into Kafka

Sometimes, you may need to collect data from any existing relational databases or messaging system into Kafka. To achieve this, you can use Kafka Connect. Kafka connect is a tool that helps to stream data reliably and durably between Kafka and other external systems. Kafka Connect can collect data continuously from any external systems into Kafka and vice versa.