当前位置: > > > > Kafka 消费者抵消导出 golang — sharma 或 confluence-kafka-go lib
来源:stackoverflow
2024-04-23 20:51:34
0浏览
收藏
编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天就整理分享《Kafka 消费者抵消导出 golang — sharma 或 confluence-kafka-go lib》,文章讲解的知识点主要包括,如果你对Golang方面的知识点感兴趣,就不要错过,在这可以对大家的知识积累有所帮助,助力开发能力的提升。
问题内容
我正在尝试找到一种对消费者组执行偏移重置操作的方法,例如在 kafka 命令中将是这样的:
kafka-consumer-groups.sh --bootstrap-server $kfk --dry-run --reset-offsets --topic $t --group $cg1 --to-current --export | tee topic-offset.csv
kafka-consumer-groups.sh --bootstrap-server $kfk --dry-run --reset-offsets --topic $t --group $cg2 --to-current
然后基于该导出文件导入新的偏移量?
kafka-consumer-groups.sh --bootstrap-server $kfk --execute --reset-offsets --topic $t --group $cg2 --from-file topic-offset.csv
从文件导出导入不是问题…只是似乎无法找到获取然后设置偏移量的方法..
有人使用 sharma
或 confluence-kafka-go
lib 来玩这个吗?
感谢您提前提供任何建议:)
正确答案
这是一些根据 ymal 配置进行偏移重置的简单操作
用于 lag 监控消费者组列表和偏移重置的命令行工具。
我们有相同的偏移量……现在为了模拟这个过程,我们将生成消息到主题并继续在消费者组propertest-cg1a11之一上消费,我们将生成5条消息并在该消费者组上消费它们,这将为我们提供以下信息:我们消费了
^c共处理了33条消息
28 + 5
./kfkgo lag using config file: ~/kfk-offsets/kfk-offset.yaml group topic partition current-offset log-end-offset lag propertest-cg1a11 propertest 0 183 183 0 propertest-cg1a11 propertest 1 165 165 0 propertest-cg1a11 propertest 2 192 192 0 propertest-cg1a11 propertest 3 177 177 0 propertest-cg1a11 propertest 4 192 192 0 propertest-cg1a11 propertest 5 169 169 0 propertest-cg1a11 propertest 6 180 180 0 propertest-cg1a11 propertest 7 164 164 0 propertest-cg1a11 propertest 8 195 195 0 propertest-cg1a11 propertest 9 188 188 0 propertest-cg1a11 propertest 10 184 184 0 propertest-cg1a11 propertest 11 184 184 0 total lag: 0 ./kfkgo lag -g propertest-cg -t propertest using config file: ~/kfk-offsets/kfk-offset.yaml group topic partition current-offset log-end-offset lag propertest-cg propertest 0 179 183 4 propertest-cg propertest 1 162 165 3 propertest-cg propertest 2 190 192 2 propertest-cg propertest 3 174 177 3 propertest-cg propertest 4 187 192 5 propertest-cg propertest 5 167 169 2 propertest-cg propertest 6 177 180 3 propertest-cg propertest 7 160 164 4 propertest-cg propertest 8 192 195 3 propertest-cg propertest 9 185 188 3 propertest-cg propertest 10 183 184 1 propertest-cg propertest 11 184 184 0 total lag: 33
现在我们将偏移量从propertest-cg再次移回到propertest-cg1a11,这将允许我们在该cg上处理相同的消息。
./kfkgo offset -m using config file: ~/kfk-offsets/kfk-offset.yaml moving
再次验证:
kafka 命令:kafka-consumer-groups.sh
propertest-cg1a11 propertest 0 179 183 4 - - - propertest-cg1a11 propertest 1 162 165 3 - - - propertest-cg1a11 propertest 2 190 192 2 - - - propertest-cg1a11 propertest 3 174 177 3 - - - propertest-cg1a11 propertest 4 187 192 5 - - - propertest-cg1a11 propertest 5 167 169 2 - - - propertest-cg1a11 propertest 6 177 180 3 - - - propertest-cg1a11 propertest 7 160 164 4 - - - propertest-cg1a11 propertest 8 192 195 3 - - - propertest-cg1a11 propertest 9 185 188 3 - - - propertest-cg1a11 propertest 10 183 184 1 - - - propertest-cg1a11 propertest 11 184 184 0 - - - consumer group 'propertest-cg' has no active members. propertest-cg propertest 0 179 183 4 - - - propertest-cg propertest 1 162 165 3 - - - propertest-cg propertest 2 190 192 2 - - - propertest-cg propertest 3 174 177 3 - - - propertest-cg propertest 4 187 192 5 - - - propertest-cg propertest 5 167 169 2 - - - propertest-cg propertest 6 177 180 3 - - - propertest-cg propertest 7 160 164 4 - - - propertest-cg propertest 8 192 195 3 - - - propertest-cg propertest 9 185 188 3 - - - propertest-cg propertest 10 183 184 1 - - - propertest-cg propertest 11 184 184 0 - - - ./kfkgo lag -g propertest-cg -t propertest using config file: ~/kfk-offsets/kfk-offset.yaml group topic partition current-offset log-end-offset lag propertest-cg propertest 0 179 183 4 propertest-cg propertest 1 162 165 3 propertest-cg propertest 2 190 192 2 propertest-cg propertest 3 174 177 3 propertest-cg propertest 4 187 192 5 propertest-cg propertest 5 167 169 2 propertest-cg propertest 6 177 180 3 propertest-cg propertest 7 160 164 4 propertest-cg propertest 8 192 195 3 propertest-cg propertest 9 185 188 3 propertest-cg propertest 10 183 184 1 propertest-cg propertest 11 184 184 0 total lag: 33 ./kfkgo lag using config file: ~/kfk-offsets/kfk-offset.yaml group topic partition current-offset log-end-offset lag propertest-cg1a11 propertest 0 179 183 4 propertest-cg1a11 propertest 1 162 165 3 propertest-cg1a11 propertest 2 190 192 2 propertest-cg1a11 propertest 3 174 177 3 propertest-cg1a11 propertest 4 187 192 5 propertest-cg1a11 propertest 5 167 169 2 propertest-cg1a11 propertest 6 177 180 3 propertest-cg1a11 propertest 7 160 164 4 propertest-cg1a11 propertest 8 192 195 3 propertest-cg1a11 propertest 9 185 188 3 propertest-cg1a11 propertest 10 183 184 1 propertest-cg1a11 propertest 11 184 184 0 total lag: 33
自述文件中的更多示例
好吧,我想我发现它只需要实现完整的解决方案,但我应该擅长
func main() { brokers := []string{bk} kfk.logger = log.new(os.stdout, "", log.lstdflags) cfg := kfk.newconfig() cfg.clientid = cid client, _ := kfk.newclient(brokers, cfg) //fmt.println(client) offsetmg, _ := kfk.newoffsetmanagerfromclient(cg, client) defer offsetmg.close() consumer, _ := kfk.newconsumerfromclient(client) defer consumer.close() partitions, _ := consumer.partitions(topic) for _, p := range partitions { pom, _ := offsetmg.managepartition(topic, p) ofs, pomstr := pom.nextoffset() fmt.printf("partition: %v -> nextoffset: %v:%s\n", p, ofs, pomstr) } fmt.println("--") }
这给了我这个输出:
partition: 0 -> nextoffset: 31: partition: 1 -> nextoffset: 30: partition: 2 -> nextoffset: 45: partition: 3 -> nextoffset: 39: partition: 4 -> nextoffset: 45: partition: 5 -> nextoffset: 39: partition: 6 -> nextoffset: 37: partition: 7 -> nextoffset: 42: partition: 8 -> nextoffset: 43: partition: 9 -> nextoffset: 35: partition: 10 -> nextoffset: 41: partition: 11 -> nextoffset: 36:
与 java 命令完全相同:
kafka-consumer-groups.sh --bootstrap-server $kfk --dry-run --reset-offsets --topic $t --group $cg1 --to-current | sort -k3 -n group topic partition new-offset propertest-cg1 propertest 0 31 propertest-cg1 propertest 1 30 propertest-cg1 propertest 2 45 propertest-cg1 propertest 3 39 propertest-cg1 propertest 4 45 propertest-cg1 propertest 5 39 propertest-cg1 propertest 6 37 propertest-cg1 propertest 7 42 propertest-cg1 propertest 8 43 propertest-cg1 propertest 9 35 propertest-cg1 propertest 10 41 propertest-cg1 propertest 11 36
所以现在剩下的就是将这些数据导出到文件并使用函数
// ResetOffset resets to the provided offset, alongside a metadata string that // represents the state of the partition consumer at that point in time. Reset // acts as a counterpart to MarkOffset, the difference being that it allows to // reset an offset to an earlier or smaller value, where MarkOffset only // allows incrementing the offset. cf MarkOffset for more details. ResetOffset(topic string, partition int32, offset int64, metadata string)
设置新的偏移量…
终于介绍完啦!小伙伴们,这篇关于《Kafka 消费者抵消导出 golang — sharma 或 confluence-kafka-go lib》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~公众号也会发布Golang相关知识,快来关注吧!