3.kafka常用命令

在 0.9.0.0 之后的 Kafka,出现了几个新变动,一个是在 Server 端增加了 GroupCoordinator 这个角色,另一个较大的变动是将 topic 的 offset 信息由之前存储在 zookeeper 上改为存储到一个特殊的 topic(__consumer_offsets)中。

本文测试版本:kafka_2.12-3.1.0

1.kafka

1.1 启动kafka

后台常驻方式,带上参数 -daemon,如:

bash 复制代码
/opt/apps/kafka/bin/kafka-server-start.sh -daemon /opt/apps/kafka/config/server.properties

指定 JMX port 端口启动,指定 jmx,可以方便监控 Kafka 集群

bash 复制代码
JMX_PORT=9991 /opt/apps/kafka/bin/kafka-server-start.sh -daemon /opt/apps/kafka/config/server.properties

1.2 停止 Kafka

bash 复制代码
/opt/apps/kafka/bin/kafka-server-stop.sh

2.Topic

2.1 创建 Topic

参数 --topic 指定 Topic 名,--partitions 指定分区数,--replication-factor 指定分区复本数:

bash 复制代码
/opt/apps/kafka/bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

注意,如果配置文件 server.properties 指定了 Kafka 在 zookeeper 上的目录,则参数也要指定,否则会报无可用的 brokers(下面部分命令也有同样的情况),如:

bash 复制代码
/opt/apps/kafka/bin/kafka-topics.sh --create --zookeeper localhost:2181/kafka --replication-factor 1 --partitions 1 --topic test

2.2 列出所有 Topic

bash 复制代码
/opt/apps/kafka/bin/kafka-topics.sh --list --zookeeper localhost:2181 

2.3 查看 Topic

bash 复制代码
/opt/apps/kafka/bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test 

2.4 增加 Topic 的 partition 数

bash 复制代码
/opt/apps/kafka/bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic test --partitions 5 

2.5 查看 topic 指定分区 offset 的最大值或最小值

time 为 -1 时表示最大值,为 -2 时表示最小值:

bash 复制代码
/opt/apps/kafka/bin/kafka-run-class.sh kafka.tools.GetOffsetShell --topic test --time -1 --broker-list 127.0.0.1:9092 --partitions 0 

2.6 删除 Topic

bash 复制代码
/opt/apps/kafka/bin/kafka-topics.sh --zookeeper localhost:2181 --topic test --delete 

3.生产消息

bash 复制代码
/opt/apps/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test

4.消费消息

从头开始

bash 复制代码
/opt/apps/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning

从尾部开始,必须指定分区

bash 复制代码
/opt/apps/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --offset latest --partition 0

指定分区

bash 复制代码
/opt/apps/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --offset latest --partition 0

取指定个数

bash 复制代码
/opt/apps/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --offset latest --partition 0 --max-messages 1

5.消费组group

指定group

bash 复制代码
/opt/apps/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test -group test_group --from-beginning

消费者group列表

bash 复制代码
/opt/apps/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list

查看group详情

bash 复制代码
/opt/apps/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group test_group --describe

执行结果:

bash 复制代码
bigdatabit9@instance-kafka01:~$ /opt/apps/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092  --group vdata.data.sink.starrocks.offline --describe

Consumer group 'vdata.data.sink.starrocks.offline' has no active members.

GROUP                             TOPIC            PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID     HOST            CLIENT-ID
vdata.data.sink.starrocks.offline mercury-schedule 0          95              95              0               -               -               -
vdata.data.sink.starrocks.offline mercury-schedule 1          59              59              0               -               -               -
vdata.data.sink.starrocks.offline mercury-schedule 2          54              54              0               -               -               -

删除group中的topic

bash 复制代码
/opt/apps/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group test_group --topic test --delete

删除group

bash 复制代码
/opt/apps/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group test_group --delete

6.平衡leader

bash 复制代码
/opt/apps/kafka/bin/kafka-preferred-replica-election.sh --bootstrap-server localhost:9092

7.自带压测工具

bash 复制代码
/opt/apps/kafka/bin/kafka-producer-perf-test.sh --topic test --num-records 100 --record-size 1 --throughput 100 --producer-props bootstrap.servers=localhost:9092