kafka常用命令
创始人
2024-02-06 19:42:50
0

文章目录

          • 一、KAFKA启停命令
            • 1. 前台启动
            • 2. 后台启动
            • 3. 停止命令
          • 二、Topic 相关命令
            • 2.1. 创建 Topic
            • 2.2. 查询 Topic 列表
            • 2.3. 查询 Topic 详情
            • 2.4. 增加 Topic 的 partition 数
            • 2.5. 查看 topic 指定分区 offset 的最大值或最小值
            • 2.6. 删除Topic
          • 三、消息 相关命令
            • 3.1. 发送消息
            • 3.2. 消费消息(从头开始)
            • 3.3. 消费消息(从尾开始)
            • 3.4. 消费消息(从尾开始指定分区)
            • 3.5. 消费消息(指定分区指定偏移量)
            • 3.6. 指定分组->消费消息
            • 3.7. 取指定个数
          • 四、消费者 Group
            • 4.1. 指定 Group
            • 4.2. 消费者 Group 列表
            • 4.3. 查看 Group 详情
            • 4.4. 删除 Group 中 Topic
            • 4.5. 删除 Group
          • 五、补充命令
            • 5.1.平衡 leader
            • 5.2. 自带压测工具

一、KAFKA启停命令
1. 前台启动

kafka 前台启动命令:

bin/kafka-server-start.sh config/server.properties
2. 后台启动

kafka 后台启动命令:
后台常驻方式,带上参数 -daemon,如:

bin/kafka-server-start.sh -daemon config/server.properties

或者

nohup bin/kafka-server-start.sh config/server.properties &

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

JMX_PORT=9991 /usr/local/kafka/bin/kafka-server-start.sh -daemon /usr/local/kafka/config/server.properties
3. 停止命令

kafka 停止命令:

bin/kafka-server-stop.sh
二、Topic 相关命令
2.1. 创建 Topic

参数 --topic 指定 Topic 名,–partitions 指定分区数,–replication-factor 指定备份(副本)数

创建名为 test_kafka_topic 的 Topic

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

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

/usr/local/kafka/bin/kafka-topics.sh --create --zookeeper localhost:2181/kafka --replication-factor 1 --partitions 1 --topic test
2.2. 查询 Topic 列表

列出所有 Topic

bin/kafka-topics.sh --list --zookeeper localhost:2181
2.3. 查询 Topic 详情

查询 Topic 的详细信息

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

说明:如果未指定 topic 则输出所有 topic 的信息

2.4. 增加 Topic 的 partition 数
bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic test_kafka_topic --partitions 5 
2.5. 查看 topic 指定分区 offset 的最大值或最小值

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

bin/kafka-run-class.sh kafka.tools.GetOffsetShell --topic test_kafka_topic --time -1 --broker-list 127.0.0.1:9092 --partitions 0 
2.6. 删除Topic

删除名为 test_kafka_topic 的 Topic

bin/kafka-topics.sh --delete --zookeeper localhost:2181  --topic test_kafka_topic

说明:在${KAFKA_HOME}/config/server.properties中配置 delete.topic.enable 为 true,这样才能生效,删除指定的 topic主题

三、消息 相关命令
3.1. 发送消息

生产者发送消息

bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test_kafka_topic
3.2. 消费消息(从头开始)

消费者查询消息

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092  --from-beginning --topic test_kafka_topic
3.3. 消费消息(从尾开始)

从尾部开始取数据

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test_kafka_topic --offset latest
3.4. 消费消息(从尾开始指定分区)

从尾部开始取数据,指定分区消费:

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test_kafka_topic --offset latest --partition 0
3.5. 消费消息(指定分区指定偏移量)

–partition 指定起始偏移量消费–offset:

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test_kafka_topic  --partition 0 --offset 100 
3.6. 指定分组->消费消息

消费者消费消息(指定分组)
注意给客户端命名之后,如果之前有过消费,那么–from-beginning就不会再从头消费了

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092  --from-beginning --topic test_kafka_topic --group t1

说明:
–from-beginning:表示从头开始接收数据
–group:指定消费者组

3.7. 取指定个数
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test_kafka_topic --offset latest --partition 0 --max-messages 1
四、消费者 Group
4.1. 指定 Group

指定分组从头开始消费消息(应该会指定偏移量)

/usr/local/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test -group test_group --from-beginning
4.2. 消费者 Group 列表
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
4.3. 查看 Group 详情
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group test_group --describe

输出日志:

Consumer group 'test_group' has no active members.TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID     HOST            CLIENT-ID
test            0          5               5               0               -               -               -# CURRENT-OFFSET: 当前消费者群组最近提交的 offset,也就是消费者分区里读取的当前位置
# LOG-END-OFFSET: 当前最高水位偏移量,也就是最近一个读取消息的偏移量,同时也是最近一个提交到集群的偏移量
# LAG:消费者的 CURRENT-OFFSET 与 broker 的 LOG-END-OFFSET 之间的差距
4.4. 删除 Group 中 Topic
bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group test_group --topic test --delete
4.5. 删除 Group
/usr/local/kafka/bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group test_group --delete
五、补充命令
5.1.平衡 leader
bin/kafka-preferred-replica-election.sh --bootstrap-server localhost:9092
5.2. 自带压测工具
bin/kafka-producer-perf-test.sh --topic test --num-records 100 --record-size 1 --throughput 100 --producer-props bootstrap.servers=localhost:9092 

相关内容

热门资讯

中证A500ETF摩根(560... 8月22日,截止午间收盘,中证A500ETF摩根(560530)涨1.19%,报1.106元,成交额...
A500ETF易方达(1593... 8月22日,截止午间收盘,A500ETF易方达(159361)涨1.28%,报1.104元,成交额1...
何小鹏斥资约2.5亿港元增持小... 每经记者|孙磊    每经编辑|裴健如 8月21日晚间,小鹏汽车发布公告称,公司联...
中证500ETF基金(1593... 8月22日,截止午间收盘,中证500ETF基金(159337)涨0.94%,报1.509元,成交额2...
中证A500ETF华安(159... 8月22日,截止午间收盘,中证A500ETF华安(159359)涨1.15%,报1.139元,成交额...
科创AIETF(588790)... 8月22日,截止午间收盘,科创AIETF(588790)涨4.83%,报0.760元,成交额6.98...
创业板50ETF嘉实(1593... 8月22日,截止午间收盘,创业板50ETF嘉实(159373)涨2.61%,报1.296元,成交额1...
港股异动丨航空股大幅走低 中国... 港股航空股大幅下跌,其中,中国国航跌近7%表现最弱,中国东方航空跌近5%,中国南方航空跌超3%,美兰...
电网设备ETF(159326)... 8月22日,截止午间收盘,电网设备ETF(159326)跌0.25%,报1.198元,成交额409....
红利ETF国企(530880)... 8月22日,截止午间收盘,红利ETF国企(530880)跌0.67%,报1.034元,成交额29.0...