Kafka 消费者抵消导出 golang -- sharma 或 confluence-kafka-go lib
来源:stackoverflow
时间:2024-04-23 20:51:34 369浏览 收藏
编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天golang学习网就整理分享《Kafka 消费者抵消导出 golang -- sharma 或 confluence-kafka-go lib》,文章讲解的知识点主要包括,如果你对Golang方面的知识点感兴趣,就不要错过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 配置进行偏移重置的简单操作
https://github.com/nXnUs25/kfk-offsets
用于 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学习网公众号也会发布Golang相关知识,快来关注吧!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习