1、查看topic列表

kafka-topics --zookeeper hostname:2181 --list

2、查看topic详情

#查看所有topic
kafka-topics --zookeeper hostname:2181 --describe
#查看指定topic
kafka-topics --zookeeper hostname:2181 --topic xxx_topic --describe

3、订阅消费

#方法一
kafka-console-consumer --zookeeper hostname:2181 --from-beginning --topic xxx_topic (如果系统默认有kafka-console-consumer 命令,可用)
#方法二
./kafka-console-consumer.sh --bootstrap-server hostname:9092 --topic xxx_topic --from-beginning

#指定offset消费
#方法一
kafka-run-class kafka.tools.SimpleConsumerShell --broker-list hostname:9092 --topic xxx_topic --partition 5 --offset 2 --max-messages 10
#方法二
kafka-run-class kafka.tools.ConsoleConsumer --bootstrap-server hostname:9092  --topic xxx_topic --partition 5 --offset 2  --max-messages 10

参数说明:
--from-beginning:从头消费,不加该参数,默认消费kafka最新的消息
--max-messages:本次消费的最大条数
--partition:分区
--offset:偏移量

kafka命令行使用_bootstrap

4、查看offset

kafka-run-class  kafka.tools.GetOffsetShell --broker-list hostname:9092 --topic xxx_topic

kafka命令行使用_bootstrap_02

5、kafka重分区

kafka集群broker增加磁盘或者增加了broker,需要把数据均衡到新磁盘或者新节点会用到kafka重分区,重分区有几种使用场景

1、均衡所有的topic

使用kafka-topics --zookeeper hostname:2181 --list获取要均衡的kafka列表,编辑topic.json

#示例
{"topics": [{"topic": "xxx_topic"}, {"topic": "xxx_xxx_topic"}], "version":1}

生成kafka均衡的计划的json文件

./kafka-reassign-partitions.sh --zookeeper hostname:2181 --topics-to-move-json-file topic.json --broker-list 60,61,62,75 --generate

参数说明
--broker-list:要均衡到哪些broker,后跟broker-id
--throttle:设置均衡的速度,单位

执行上面的命令会输出

Current partition replica assignment
xxx
Proposed partition reassignment configuration
xxx

把刚才 Proposed partition reassignment configuration 的结果拷贝到 partitions_to_move.json 文件里面,然后执行kafka均衡

./kafka-reassign-partitions.sh --zookeeper hostname:2181 --reassignment-json-file partitions_to_move.json --execute
当出现下面的日志,说明已经迁移成
Reassignment of partition xxx_topic-6 completed successfully

查看均衡进度

./kafka-reassign-partitions.sh --zookeeper hostname:2181 --reassignment-json-file partitions_to_move.json --verify

2、均衡某个topic的某个分区

编辑custom-reassignment.json,生成kafka均衡的计划

{"version":1,"partitions":[{"topic":"xxx_xxxtopic","partition":2,"replicas":[1001,1002],"log_dirs":["/seqdata01/kafka/data","/seqdata02/kafka/data"]}]}

说明:
topic:要均衡的topic
partition:要均衡的分区
replicas:要均衡到那几个broker,填写broker-id
logdirs:分区要写入到broker的哪个目录,和前面的replicas一一对应,示例中为均衡到broker-ids 1001的/seqdata01/kafka/data目录和broker-ids 1002的/seqdata02/kafka/data,不写的话默认为any

执行均衡

bin/kafka-reassign-partitions.sh --zookeeper hostname:2181 --reassignment-json-file custom-reassignment.json --execute

查看均衡进度

bin/kafka-reassign-partitions.sh --zookeeper hostname:2181 --reassignment-json-file custom-reassignment.json --verify