Using Kafka under Windows

Posted by daverules on Mon, 20 Jan 2020 18:22:24 +0100

1, Prepare ZooKeeper

Download the latest stable version from the official website: 3.5.6 . After downloading, extract it to the local location, find zoo_sample.cfg in the conf directory, rename it to zoo.cfg, and modify the data storage directory in it:

#dataDir=/tmp/zookeeper
dataDir=D:/hecg/apache-zookeeper-3.5.6-bin/data

Start with git command line client:

## Test start, normal
$ ./zkServer.sh start
/c/ProgramData/Oracle/Java/javapath/java
ZooKeeper JMX enabled by default
Using config: D:\hecg\apache-zookeeper-3.5.6-bin\conf\zoo.cfg
Starting zookeeper ... STARTED
## Test stop, normal
$ ./zkServer.sh stop
/c/ProgramData/Oracle/Java/javapath/java
ZooKeeper JMX enabled by default
Using config: D:\hecg\apache-zookeeper-3.5.6-bin\conf\zoo.cfg

2, Prepare Kafka

The version downloaded from the official website is: kafka_2.12-2.4.0

2.1 modify configuration file
  • Modify the kafka configuration file in the config directory and the log storage directory:

    #log.dirs=/tmp/kafka-logs
    log.dirs=D:/Program/kafka_2.12-2.4.0/data/kafka-logs
    
  • Modify the zookeeper configuration file in the config directory, and modify the data storage directory:

    #dataDir=/tmp/zookeeper
    dataDir=D:/hecg/kafka_2.12-2.4.0/data/zookeeper
    
2.2 start ZooKeeper:
$ bin/zookeeper-server-start.sh config/zookeeper.properties
[2020-01-20 14:46:01,282] INFO Reading configuration from: config/zookeeper.properties (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
[2020-01-20 14:46:01,283] WARN config\zookeeper.properties is relative. Prepend.\ to indicate that you're sure! (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
[2020-01-20 14:46:01,284] INFO clientPortAddress is 0.0.0.0/0.0.0.0:2181 (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
...
2.3 start Kafka Server:
$ bin/kafka-server-start.sh config/server.properties
[2020-01-20 14:50:57,961] INFO Registered kafka:type=kafka.Log4jController MBean (kafka.utils.Log4jControllerRegistration$)
[2020-01-20 14:50:58,388] INFO starting (kafka.server.KafkaServer)
[2020-01-20 14:50:58,389] INFO Connecting to zookeeper on localhost:2181 (kafka.server.KafkaServer)
[2020-01-20 14:50:58,407] INFO [ZooKeeperClient Kafka server] Initializing a new session to localhost:2181. (kafka.zookeeper.ZooKeeperClient)
[2020-01-20 14:50:58,412] INFO Client environment:zookeeper.version=3.5.6-c11b7e26bc554b8523dc929761dd28808913f091, built on 10/08/2019 20:18 GMT (org.apache.zookeeper.ZooKeeper)
...
2.4 create Topic
  • To create a Topic:

    $ bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test
    
  • List all topics:

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

During this period, there is a little episode: the configuration file cannot be loaded into log4j: after the configuration file is written, it is ready

$ bin/kafka-topics.sh --list --bootstrap-server localhost:9092
log4j:ERROR Could not read configuration file from URL [file:/d/hecg/kafka_2.12-2.4.0/bin/../config/tools-log4j.properties].
java.io.FileNotFoundException: \d\hecg\kafka_2.12-2.4.0\bin\..\config\tools-log4j.properties
...

## Modify kafka-run-class.sh line 194
#LOG4J_DIR="$base_dir/config/tools-log4j.properties"
LOG4J_DIR="D:/hecg/kafka_2.12-2.4.0/config/tools-log4j.properties"
2.5 sending and receiving message test
  • Specify Topic to send message:

    $ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
    >This is a message
    >This is another message
    >hello hecg
    >
    
  • Specify the Topic to receive the message:

    $ bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
    This is a message
    This is another message
    hello hecg
    

Topics: Programming Zookeeper kafka Apache log4j