获取topic当前消息数

创建测试 topic:
bin/kafka-topics.sh -zookeeper localhost:2181 --create --partitions 5 --replication-factor 1 --topic test765

生产测试消息:
为 topic test765 生产 500000 条消息:
bin/kafka-producer-perf-test.sh --topic test765 --throughput -1 --record-size 10 --num-records 500000 --producer-props bootstrap.servers=localhost:9092

获取 topic 消息数:
bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic test765 --time -1

输出信息

test765:0:100000
test765:1:100000
test765:2:100000
test765:3:100000
test765:4:100000

bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic test765 --time -2

输出信息

test765:0:0
test765:1:0
test765:2:0
test765:3:0
test765:4:0

–time-1 表示要获取指定topic所有分区当前的最大位移,–time-2 表示获取当前最早位移。

两个命令的输出结果相减便可得到所有分区当前的消息总数。

分区当前的消息总数 = [–time-1] - [–time-2]

相减是因为随着 kafka 的运行,topic 中有的消息可能会被删除,,因此 --time-1 的结果其实表示的是历史上该topic生产的最大消息数,如果用户要统计当前的消息总数就必须减去 --time-2 的结果。

本例中没有任何消息被删除,故 --time-2 的结果全是0,表示最早位移都是0,消息总数等于历史上发送的消息总数。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值