1、开启zookeeper(在安装目录下使用命令)

Linux:bin/zkServer.sh start

windows:bin\zkServer.cmd

2、启动kafka(安装目录下使用命令)

Linux:bin/kafka-server-start.sh start config/server.properties

windows:bin\windows\kafka-server-start.bat config\server.properties

3、查看topic名称列表

Linux:bin/kafka-topics.sh -list --zookeeper 172.16.0.99:2181,172.16.0.218:2181

windows:bin\windows\kafka-topics.bat -list --zookeeper 172.16.0.99:2181,172.16.0.218:2181

4、查看topic详情

Linux:bin/kafka-topics.sh -zookeeper localhost:2181 --topic test --describe

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

5、删除topic

Linux:bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic "test"

windows:bin\windows\kafka-topics.bat --zookeeper localhost:2181 --delete --topic "test"

注意:集群中一台机器删除了topic,其他机器同步删除相同topic

6、创建topic

Linux:bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

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

注意:

replication-factor:副本个数,一般为小于等于Broker个数。

partitions:分区个数。如果副本个数为1,分区为4,则4个分区会均匀的分布在各个Broker上。如果Broker为2,副本为2,分区为4,则每个Broker上面都有4个分区。

7、创建Consumer

Linux:bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning

windows:bin\windows\kafka-console-consumer.bat --zookeeper localhost:2181 --topic test --form-beginning

注意:form beginning表示从头拉取。

8、创建Producer

Linux:bin/kafka-console-producer.sh --broker-list 172.16.0.99:9020,172.16.0.218:9020 --topic test

windows:bin\windows\kafka-console-producer.bat --broker-list 172.16.0.99:9092,172.16.0.218:9080 --topic test

注意:此处是kafka的端口,而且在集群里如果此处填localhost,会报一个连接错误,猜想应该是消息没有到达集群,因此此处将集群的ip都填上。

9、查询topic所有分区的offset值

Linux:bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list 10.162.160.115:9092 --topic s1mmetest --time -1

10、查询kafka集群当前topic所有分区中的消息数目

Linux:bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list 132.232.78.175:9092 --topic s1mmetest --time -2

参考:https://www.2cto.com/kf/201804/739331.html

07-06 04:34