当前位置:首页 > 文章列表 > 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互联网时代的弄潮儿。
    516次学习
  • 简单聊聊mysql8与网络通信
    简单聊聊mysql8与网络通信
    如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
    500次学习
  • JavaScript正则表达式基础与实战
    JavaScript正则表达式基础与实战
    在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
    487次学习
  • 从零制作响应式网站—Grid布局
    从零制作响应式网站—Grid布局
    本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
    485次学习
查看更多
AI推荐
  • ChatExcel酷表:告别Excel难题,北大团队AI助手助您轻松处理数据
    ChatExcel酷表
    ChatExcel酷表是由北京大学团队打造的Excel聊天机器人,用自然语言操控表格,简化数据处理,告别繁琐操作,提升工作效率!适用于学生、上班族及政府人员。
    3191次使用
  • Any绘本:开源免费AI绘本创作工具深度解析
    Any绘本
    探索Any绘本(anypicturebook.com/zh),一款开源免费的AI绘本创作工具,基于Google Gemini与Flux AI模型,让您轻松创作个性化绘本。适用于家庭、教育、创作等多种场景,零门槛,高自由度,技术透明,本地可控。
    3403次使用
  • 可赞AI:AI驱动办公可视化智能工具,一键高效生成文档图表脑图
    可赞AI
    可赞AI,AI驱动的办公可视化智能工具,助您轻松实现文本与可视化元素高效转化。无论是智能文档生成、多格式文本解析,还是一键生成专业图表、脑图、知识卡片,可赞AI都能让信息处理更清晰高效。覆盖数据汇报、会议纪要、内容营销等全场景,大幅提升办公效率,降低专业门槛,是您提升工作效率的得力助手。
    3434次使用
  • 星月写作:AI网文创作神器,助力爆款小说速成
    星月写作
    星月写作是国内首款聚焦中文网络小说创作的AI辅助工具,解决网文作者从构思到变现的全流程痛点。AI扫榜、专属模板、全链路适配,助力新人快速上手,资深作者效率倍增。
    4541次使用
  • MagicLight.ai:叙事驱动AI动画视频创作平台 | 高效生成专业级故事动画
    MagicLight
    MagicLight.ai是全球首款叙事驱动型AI动画视频创作平台,专注于解决从故事想法到完整动画的全流程痛点。它通过自研AI模型,保障角色、风格、场景高度一致性,让零动画经验者也能高效产出专业级叙事内容。广泛适用于独立创作者、动画工作室、教育机构及企业营销,助您轻松实现创意落地与商业化。
    3812次使用
微信登录更方便
  • 密码登录
  • 注册账号
登录即同意 用户协议隐私政策
返回登录
  • 重置密码