本需求是工作过程中,使用kafka中间件的分析任务速度过慢,排查问题原因时产生,需要获取kafka中的数据积压量(已经数据量-消费者组已消费数据量)。废话不多说,直接上代码。
package main
import (
"fmt"
"os"
"strings"
"github.com/IBM/sarama"
)
func Usage() {
println("输入有误,使用格式为: ./backlog kafkaAddress topic groupId")
println("示例: ./backlog 10.67.54.24:9092,10.67.54.25:9093 test_in consumer_test_in")
}
func main() {
if len(os.Args) != 4 {
Usage()
os.Exit(1)
}
kafkaAddress := os.Args[1] // kafka 地址
topic := os.Args[2] // 主题
groupId := os.Args[3] // 消费者组
broker := strings.Split(kafkaAddress, ",")
// 获取分区数
config := sarama.NewConfig()
client, err := sarama.NewClient(broker, config)
if err != nil {
panic(err.Error())
}
defer client.Close()
partitions, err := client.Partitions(topic)
if err != nil {
panic(err.Error())
}
om, err := sarama.NewOffsetManagerFromClient(groupId, client)
if err != nil {
panic(err.Error())
}
var totalBacklog int64 = 0
for _, partition := range partitions {
//

本文介绍了如何使用Sarama库在Go语言中监控Kafka主题上的数据积压情况,通过获取分区的offset来计算未被消费者组消费的数据量。
最低0.47元/天 解锁文章
6164

被折叠的 条评论
为什么被折叠?



