Mục lục [Click vào để xem]
This tutorial assumes you are starting fresh and have no existing Kafka or ZooKeeper data.
Refer: https://kafka.apache.org/quickstart
Install and start Kafka
# 1. Download Kafka and Install | |
wget http://ftp.jaist.ac.jp/pub/apache/kafka/2.2.0/kafka_2.12-2.2.0.tgz | |
tar -xzf kafka_2.12-2.2.0.tgz | |
cd kafka_2.12-2.2.0 | |
# 2. Start Zookeeper server (in new terminal) | |
bin/zookeeper-server-start.sh config/zookeeper.properties & | |
# 3. Start Kafka server (in new terminal) | |
bin/kafka-server-start.sh config/server.properties & |
Use any features of Kafka
# Create topic with named "test" | |
bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic | |
# See topic test | |
bin/kafka-topics.sh --list --bootstrap-server localhost:9092 test |
Kafka comes with a command line client that will take input from a file or from standard input and send it out as messages to the Kafka cluster. By default, each line will be sent as a separate message.
Run the producer and then type a few messages into the console to send to the server.
> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test | |
Message 1 | |
Message 2 |
Kafka also has a command line consumer that will dump out messages to standard output.
> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning | |
Message 1 | |
Message 2 |
If you have each of the above commands running in a different terminal then you should now be able to type messages into the producer terminal and see them appear in the consumer terminal.
All of the command line tools have additional options; running the command with no arguments will display usage information documenting them in more detail.