kafka常用命令

相关概念
Broker

kafka节点,多个broker组成kafka集群。

Topic

即主题,kafka通过Topic对消息进行分类,发布到kafka的消息都需要指定Topic。

Producer

即消息生产者,向Broker发送消息的客户端。

Consumer

即消息消费者,从Broker消费消息的客户端。

ConsumerGroup

即消费者组,消费者隶属于消费者组,同一个分区的消息可以被多个消费者消费,但是同一个消费者组中只能有一个消费者可以消费。

Partition

即分区,每个Topic下都至少有一个分区,分区内部的消息是有序的。

------------------------------------------------------------------------------------------

查看kafka集群是否启动成功

启动zk客户端连接:

./zkCli.sh -server zk-IP:2181
查看有几个kafka启动了,这里查看的是Kafka的broker.id。
ls /brokers/ids
如果还想查看单个Kafka节点的状态,就指定好broker.id查看,例如查看broker.id=0的那个Kafka节点的状态。
get /brokers/ids/0
————————————————

1、启动kafka服务

bin/kafka-server-start.sh config/server.properties &
2、停止kafka服务  ./kafka-server-stop.sh
3、创建一个叫demo-topic的主题(topic),有两个分区,每个分区3个副本,同时指定该主题的消息保留时长(72小时)

./kafka-topics.sh --zookeeper(host:port) --create --topic demo-topic --replication-factor 3 --partitions 2 --config retention.ms=259200000
4、列出指定主题(topic)的详细信息

./kafka-topics.sh  --zookeeper(host:port) --describe  --topic demo-topic 
5、查看所有的主题

./kafka-topics.sh --list --zookeeper(host:port) kafka-host(host:port)
6、查看所有主题的详细信息

./kafka-topics.sh --zookeeper(host:port) --describe
7、删除一个主题

./kafka-topics.sh --zookeeper(host:port) --topic demo-topic --delete 
8.向kafka指定topi写入数据

./kafka-console-producer.sh --broker-list kafka-host(host:port)--topic demo-topic
9、命令行消费某个topic消息

#加了--from-beginning 从头消费所有消息
./kafka-console-consumer.sh --bootstrap-server kafka-host(host:port) --topic demo-topic --from-beginning   
#不加--from-beginning 从最新的一条消息开始消费
./kafka-console-consumer.sh --bootstrap-server kafka-host(host:port) --topic demo-topic 
10、查看某个topic对应的消息数量

# time为-1时表示最大值,time为-2时表示最小值 --partitions num 指定分区
./kafka-run-class.sh  kafka.tools.GetOffsetShell --broker-list kafka-host(host:port) --topic demo-topic --time -1
11、kafka重置分组已经消费的偏移量offest

./kafka-consumer-groups.sh --bootstrap-server=kafka-host(host:port) --execute --reset-offsets --topic=demo-topic --group=testPlatform --to-earliest
12、topic增加分区

./kafka-topics.sh --alter --zookeeper(host:port) --topic demo-topic --partitions 12

13、指定topic创建消费者分组

./kafka-console-consumer.sh  --bootstrap-server=kafka-host(host:port) --topic demo-topic --consumer-property group.id=testPlatform
14、查看消费组组所属topic的消费情况

./kafka-consumer-groups.sh --bootstrap-server=kafka-host(host:port)  --group=demo-group --describ
15、显示所有消费者

./kafka-consumer-groups.sh --bootstrap-serverkafka-host(host:port) --list
16、获取正在消费的topic的group的offset

./kafka-consumer-groups.sh --describe --group demo-group --bootstrap-serverkafka-host(host:port)
17、重设 consumer group的offset

确定topic作用域:
 --all-topics   为consumer group下所有topic的所有分区调整位移
 --topic t1 --topic t2  为指定的若干个topic的所有分区调整位移
 --topic t1:0,1,2     为指定的topic分区调整位移

确定位移重设策略
--to-current                            把位移调整到分区当前位移.       
--to-datetime <String: datetime>       把位移调整到大于给定时间的最早位移处.datetime. Format: 'YYYY-MM-DDTHH:mm:SS.sss'  2020-07-01T12:00:00.000
--to-earliest                           把位移调整到分区当前最小位移
--to-latest                             把位移调整到分区当前最新位移        
--to-offset <Long: offset>              把位移调整到指定位移处
--shift-by N: 把位移调整到当前位移 + N处,注意N可以是负数,表示向前移动
--by-duration <duration>:把位移调整到距离当前时间指定间隔的位移处,duration格式是PnDTnHnMnS,比如PT0H5M0S
--from-file <file>:从CSV文件中读取调整策略
例:(--dry-run 不运行只查看结果,类似k8s里面;--execute执行)
按时间点重置(--to-datetime)
./kafka-consumer-groups.sh --bootstrap-server=localhost:9092 --topic=test --group=testPlatform  --execute --reset-offsets --to-datetime 2022-01-18T12:00:00.000 

按offset重置 (--to-offset )
重置消费组下指定topic
./kafka-consumer-groups.sh --bootstrap-server=localhost:9092 --topic=test --group=testPlatform --execute --reset-offsets --to-offset 359905139  

重置消费组下面所以topic
./kafka-consumer-groups.sh --bootstrap-server=localhost:9092 --all-topics --group testPlatform --reset-offsets  --to-latest  --execute


18、指定offset与partition导出消息

./kafka-console-consumer.sh --bootstrap-server=kafka-host(host:port) --topic --topic=demo-topic  --offset 825000 --partition 0 >> messages.log
19.修改topic的参数

kafka-configs.sh --zookeeper(host:port) --entity-type topics --entity-name demo-topic --alter --add-config max.message.bytes=1048576

20.测试生产者性能脚本

./kafka-producer-perf-test.sh --topic demo-topic --num-records 10000000 --throughput -1 --record-size 1024 --producer-props bootstrap.servers=kafka-host(host:port) acks=-1 linger.ms=2000 compression.type=lz4

21.测试消费者性能脚本

./kafka-consumer-perf-test.sh --broker-list kafka-host(host:port) --messages 10000000 --topic demo-topic

22.查看所有消费者组

./kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list

23.扩容分区

#把liNingShortSleeveTopic主题扩容为6个分区。
#注意:目前不支持减少分区,扩容前必须存在这个主题。
./kafka-topics.sh -alter --partitions 6 --bootstrap-server localhost:9092 --topic liNingShortSleeveTopic

posted @   treesnail  阅读(967)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示