kafka相关命令详解

一、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-idtest            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   


Delfast正研发一款定位于中小型企业的电动三轮车 将搭载模块化货运平台
一文看懂喇叭阻抗4欧8欧区别
卢奇骏:决策权衡影响TD-SCDMA市场化进程
测量工作的原则及基本的测量工作
蔡奇:打造北京改革开放新高地
kafka相关命令详解
新型楼宇对讲机的分机各部分的设计与测试研究
协作机器人从概念化产品已成为工业自动化产业中增长最快的领域
红外避障模块电路图
微软Windows Store出现了Galaxy Book应用的消息或暗示三星Galaxy Book即将发布
PCIM Europe 的电力电子创新
rk3399和骁龙650对比
光电耦合器传输速度的提高
同轴喇叭是什么意思_同轴喇叭的优缺点
怎样用AT89S52单片机设计及制作智能遥控开锁装置
Marvell即将以100亿美元的价格收购Inphi
申盛科技:打造高品质产品,质量可靠用户青睐
测肥料浓度仪器设备的特点介绍
Arduino提供的PWM控制功能入门(2)
中国政府支持开展车联网和自动驾驶地图应用