Kafka 常用命令记录

操作 kafka 时记不住shell命令、肿么办?

1、查看当前的集群Topic列表
[root@node173 ~]# cd /usr/hdp/3.0.1.0-187/kafka/bin
[root@node173 bin]# ./kafka-topics.sh --list --zookeeper node173.data:2181
输出为
__consumer_offsets
ambari_kafka_service_check
demo

2、查看所有的Topic的详细信息
./bin/kafka-topics.sh --describe --zookeeper node173.data:2181/kafka
如果要查看单个 topic 信息:可在上述命令后面添加 --topic $topicName
./bin/kafka-topics.sh --describe --zookeeper node173.data:2181/kafka --topic demo

3、创建Topic
./bin/kafka-topics.sh --create --zookeeper node173.data:2181/kafka --replication-factor 3 --partitions 1 --topic test-topic
参数说明: --topic 主题名 --replication-factor 3 定义副本数(不能超过broker数) --partitions 1 定义分区数
cd /usr/hdp/3.0.1.0-187/kafka/logs 可以看见topic+分区号(0,1,2……)的文件。该路径下的server.log非常重要哦!进程丢失即可来此观看。

4、删除Topic
删除 topic 之前,需要确保配置delete.topic.enable=true
./bin/kafka-topics.sh --delete --zookeeper node173.data:2181/kafka --topic topic-demo Topic topic-demo is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.
执行完命令后,查看 log.dirs 指定的文件目录,会发现 topic-demo 的文件夹都被标记为 -delete 。等一定的时间(根据 log.retention.check.interval.ms 配置而定,hdp 版本默认为 60s)后,被标记为 delete 的文件则会被移除。

5、生产数据
./bin/kafka-console-producer.sh --broker-list node173.data:6667 --topic test-topic

6、消费数据
./bin/kafka-console-consumer.sh --bootstrap-server node173.data:6667 --topic test-topic --from-beginning
–from-beginning 表示从最初的未过期的 offset 处开始消费数据。不加该参数,表示从最新 offset 处开始消费数据。

7、查询topic的offset范围
查询offset的最小值:
./bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list node173.data:6667 -topic test-topic --time -2
输出为
test-topic:0:0
查询offset的最大值:
./bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list node173.data:6667 -topic test-topic [--time -1]
输出为
test-topic:0:655
从上面的输出可以看出 test-topic 只有一个 Partition:0;offset 的范围是【0,655】。

8、增加分区
将分区数增加到 3 个:
./bin/kafka-topics.sh --alter --zookeeper node173.data:2181/kafka --topic test-topic --partitions 3

9、均衡 kafka 的 leader 副本
10、查看消费组
./bin/kafka-consumer-groups.sh --bootstrap-server node173.data:6667--list
查看指定消费组的详情(比如消费进度 LAG ),这里的消费者组名为 console-consumer-3665 :
./bin/kafka-consumer-groups.sh --bootstrap-server node173.data:6667 --group console-consumer-3665 --describe

11、指定 partition 和 offset 消费
./bin/kafka-console-consumer.sh --bootstrap-server node173.data:6667--topic test-topic --partition 0 --offset 1663520

12、从__consumer_offsets主题查找某个group的偏移量
1)计算 group.id 对应的 partition
__consumer_offsets 默认有 50 个 partition ,需要先计算 group.id 对应的 partition ,计算公式如下
Math.abs(groupid.hashCode()) % numPartitions
groupid 为 console-consumer-3665,numPartitions 是 50。
Math.abs(“console-consumer-3665”.hashCode()) % 50 得到的数字,就是你消费者组对应的 partition 。
2)消费分区
找到 partition 后,就可以消费指定分区了:
./bin/kafka-console-consumer.sh \ --bootstrap-server node173.data:6667 \ --topic __consumer_offsets \ --formatter "kafka.coordinator.group.GroupMetadataManager\$OffsetsMessageFormatter" \ --partition 17 | grep xxx
注意事项:
在 kafka 0.11.0.0 版本之前 --formatter 需要使用 kafka.coordinator.GroupMetadataManager\$OffsetsMessageFormatter,0.11.0.0 版本以后(含)使用上面脚本中使用的 Class 。

13、为 topic 设置单独配置
为 test-topic 设置某配置参数。
./bin/kafka-configs.sh --zookeeper node173.data:2181/kafka --entity-type topics --entity-name test-topic --alter --add-config max.message.bytes=10485760
查看这个 topic 设置的参数:
./bin/kafka-configs.sh --zookeeper node173.data:2181/kafka --entity-type topics --entity-name test-topic --describe

14、查看 kafla 数据 xxx.log 日志
./bin/kafka-run-class.sh kafka.tools.DumpLogSegments --files /data/kafka_data/logs/test-0/00000000000001049942.log --print-data-log --deep-iteration > secLog.log

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独狐游清湖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值