当前位置:首页 > 文章列表 > 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基础的同学学习。
    543次学习
  • GO语言核心编程课程
    GO语言核心编程课程
    本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
    514次学习
  • 简单聊聊mysql8与网络通信
    简单聊聊mysql8与网络通信
    如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
    499次学习
  • JavaScript正则表达式基础与实战
    JavaScript正则表达式基础与实战
    在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
    487次学习
  • 从零制作响应式网站—Grid布局
    从零制作响应式网站—Grid布局
    本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
    484次学习
查看更多
AI推荐
  • SEO  AI Mermaid 流程图:自然语言生成,文本驱动可视化创作
    AI Mermaid流程图
    SEO AI Mermaid 流程图工具:基于 Mermaid 语法,AI 辅助,自然语言生成流程图,提升可视化创作效率,适用于开发者、产品经理、教育工作者。
    178次使用
  • 搜获客笔记生成器:小红书医美爆款内容AI创作神器
    搜获客【笔记生成器】
    搜获客笔记生成器,国内首个聚焦小红书医美垂类的AI文案工具。1500万爆款文案库,行业专属算法,助您高效创作合规、引流的医美笔记,提升运营效率,引爆小红书流量!
    144次使用
  • iTerms:一站式法律AI工作台,智能合同审查起草与法律问答专家
    iTerms
    iTerms是一款专业的一站式法律AI工作台,提供AI合同审查、AI合同起草及AI法律问答服务。通过智能问答、深度思考与联网检索,助您高效检索法律法规与司法判例,告别传统模板,实现合同一键起草与在线编辑,大幅提升法律事务处理效率。
    184次使用
  • TokenPony:AI大模型API聚合平台,一站式接入,高效稳定高性价比
    TokenPony
    TokenPony是讯盟科技旗下的AI大模型聚合API平台。通过统一接口接入DeepSeek、Kimi、Qwen等主流模型,支持1024K超长上下文,实现零配置、免部署、极速响应与高性价比的AI应用开发,助力专业用户轻松构建智能服务。
    142次使用
  • 迅捷AIPPT:AI智能PPT生成器,高效制作专业演示文稿
    迅捷AIPPT
    迅捷AIPPT是一款高效AI智能PPT生成软件,一键智能生成精美演示文稿。内置海量专业模板、多样风格,支持自定义大纲,助您轻松制作高质量PPT,大幅节省时间。
    171次使用
微信登录更方便
  • 密码登录
  • 注册账号
登录即同意 用户协议隐私政策
返回登录
  • 重置密码