当前位置:首页 > 文章列表 > Golang > Go问答 > Kafka 消费者抵消导出 golang -- sharma 或 confluence-kafka-go lib

Kafka 消费者抵消导出 golang -- sharma 或 confluence-kafka-go lib

来源:stackoverflow 2024-04-23 20:51:34 0浏览 收藏

编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天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

从文件导出导入不是问题...只是似乎无法找到获取然后设置偏移量的方法..

有人使用 sharmaconfluence-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相关知识,快来关注吧!

版本声明
本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
PHP 函数在移动应用程序开发中的高级用法PHP 函数在移动应用程序开发中的高级用法
上一篇
PHP 函数在移动应用程序开发中的高级用法
法拉第未来就租金问题与房东和解,保住总部大楼使用权
下一篇
法拉第未来就租金问题与房东和解,保住总部大楼使用权
查看更多
最新文章
查看更多
课程推荐
  • 前端进阶之JavaScript设计模式
    前端进阶之JavaScript设计模式
    设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
    542次学习
  • GO语言核心编程课程
    GO语言核心编程课程
    本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
    511次学习
  • 简单聊聊mysql8与网络通信
    简单聊聊mysql8与网络通信
    如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
    498次学习
  • JavaScript正则表达式基础与实战
    JavaScript正则表达式基础与实战
    在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
    487次学习
  • 从零制作响应式网站—Grid布局
    从零制作响应式网站—Grid布局
    本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
    484次学习
查看更多
AI推荐
  • AI边界平台:智能对话、写作、画图,一站式解决方案
    边界AI平台
    探索AI边界平台,领先的智能AI对话、写作与画图生成工具。高效便捷,满足多样化需求。立即体验!
    418次使用
  • 讯飞AI大学堂免费AI认证证书:大模型工程师认证,提升您的职场竞争力
    免费AI认证证书
    科大讯飞AI大学堂推出免费大模型工程师认证,助力您掌握AI技能,提升职场竞争力。体系化学习,实战项目,权威认证,助您成为企业级大模型应用人才。
    424次使用
  • 茅茅虫AIGC检测:精准识别AI生成内容,保障学术诚信
    茅茅虫AIGC检测
    茅茅虫AIGC检测,湖南茅茅虫科技有限公司倾力打造,运用NLP技术精准识别AI生成文本,提供论文、专著等学术文本的AIGC检测服务。支持多种格式,生成可视化报告,保障您的学术诚信和内容质量。
    561次使用
  • 赛林匹克平台:科技赛事聚合,赋能AI、算力、量子计算创新
    赛林匹克平台(Challympics)
    探索赛林匹克平台Challympics,一个聚焦人工智能、算力算法、量子计算等前沿技术的赛事聚合平台。连接产学研用,助力科技创新与产业升级。
    662次使用
  • SEO  笔格AIPPT:AI智能PPT制作,免费生成,高效演示
    笔格AIPPT
    SEO 笔格AIPPT是135编辑器推出的AI智能PPT制作平台,依托DeepSeek大模型,实现智能大纲生成、一键PPT生成、AI文字优化、图像生成等功能。免费试用,提升PPT制作效率,适用于商务演示、教育培训等多种场景。
    570次使用
微信登录更方便
  • 密码登录
  • 注册账号
登录即同意 用户协议隐私政策
返回登录
  • 重置密码