Kafka: 下载安装和启动

Step 1: 下载Kafka

到官网下载后缀名为.tgz的文件,kafka_2.13-2.5.0.tgz,tgz是压缩文件,Windows需解压两次。

 

Step 2: Start Zookeeper service

启动kafka前需要先启动Zookeeper,CMD到kafka文件路径下

• cd {KAFAK_HOME}

# start zookeeper service (port: 2181)
bin\windows\zookeeper-server-start.bat config\zookeeper.properties

 

Step 3: Start Kafka service

 再开另一个CMD窗口

• cd {KAFAK_HOME}

# start kafka broker serivce (port: 9092)
bin\windows\kafka-server-start.bat config\server.properties

 

Step 3: Create a Topic

 再开另一个CMD窗口,创建一个只有1个partition(分区)和1个replication-factor(备份),叫做test的topic

• cd {KAFAK_HOME}

# create a new kafka topic "test"
bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

 

 创建好之后,可以通过运行以下命令,查看已创建的topic信息:

# list out all kafka topic
bin\windows\kafka-topics.bat --list --zookeeper localhost:2181

 还可以用describe 命令查看更详细的topic信息

# describe specific kafka topic "test"
bin\windows\kafka-topics.bat --describe --zookeeper localhost:2181 --topic test

 

Step 4: 发送消息和消费消息

使用控制台producer 将数据发布到主题,同时打开两个命令窗口,一个运行生产者,一个运行消费者,这样就可以实时观察到发送和消费的动作。

发送消息:

# publish data to specific kafka topic "test"
bin\windows\kafka-console-producer.bat --broker-list localhost:9092 --topic test

 

 

 消费消息:

# subscribe specific kafka topic "test"
bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginning

 

还可以发送带有Key的value到topic

发送消息:

# publish data with key & value to specific kafka topic "test"
bin\windows\kafka-console-producer.bat --broker-list localhost:9092 ^
--topic test ^
--property "parse.key=true" ^
--property "key.separator=:"

 

 消费消息:

# subscribe specific kafka topic "test" with key & value displayed
bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginning --property print.key=true --property key.separator=":"

 有key的显示key值,之前发送没有key的显示nul

posted @ 2020-05-18 17:42  帅气型男  阅读(674)  评论(0编辑  收藏  举报