一. 首先啟動(dòng)zookeeper和kafka
-
前臺(tái)方式啟動(dòng)zookeeper
bin/zkServer.sh start-foreground
-
啟動(dòng)kafka
bin/kafka-server-start.sh config/server.properties
二. topic命令行工具
向kafka發(fā)送消息废恋、或從kafka接收消息時(shí)必須指定topic稼病。
下面的命令可以查看kafka中已有的topic:
bin/kafka-topics.sh --zookeeper localhost:2181 --list
其中--zookeeper localhost:2181
參數(shù)是必須的。
-
創(chuàng)建一個(gè)topic(
--create
)bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic testtopic
-
查看topic的信息(
--describe
)bin/kafka-topics.sh --describe --topic testtopic --zookeeper localhost:2181
可以看到partition數(shù)為1,replicationfactor為1乍狐,因?yàn)閯?chuàng)建testtopic時(shí)指定的參數(shù)如下:
--replication-factor 1 --partitions 1
三. 存儲(chǔ)在zookeeper中的數(shù)據(jù)
kafka的topic信息存儲(chǔ)在zookeeper中赠摇。
-
使用
zkCli.sh
命令行工具可以連接到zookeeper server。bin/zkCli.sh -server localhost:2181
zookeeper是樹形結(jié)構(gòu)浅蚪,有一個(gè)根節(jié)點(diǎn)
/
藕帜。 下面的命令可以查看根節(jié)點(diǎn)下所有的子節(jié)點(diǎn)。
-
topic信息存放在
/config/topics
路徑下惜傲,可以看到剛才創(chuàng)建的testtopic洽故。ls /config/topics