{"id":207596,"date":"2025-07-08T12:48:51","date_gmt":"2025-07-08T04:48:51","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207596\/"},"modified":"2025-07-08T12:48:51","modified_gmt":"2025-07-08T04:48:51","slug":"kafka-%e6%b6%88%e8%b4%b9%e8%80%85%e6%8a%b5%e6%b6%88%e5%af%bc%e5%87%ba-golang-sharma-%e6%88%96-confluence-kafka-go-lib","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207596\/","title":{"rendered":"Kafka \u6d88\u8d39\u8005\u62b5\u6d88\u5bfc\u51fa golang &#8212; sharma \u6216 confluence-kafka-go lib"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>Kafka \u6d88\u8d39\u8005\u62b5\u6d88\u5bfc\u51fa golang &#8212; sharma \u6216 confluence-kafka-go lib<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 20:51:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300aKafka \u6d88\u8d39\u8005\u62b5\u6d88\u5bfc\u51fa golang &#8212; sharma \u6216 confluence-kafka-go lib\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9Golang\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u627e\u5230\u4e00\u79cd\u5bf9\u6d88\u8d39\u8005\u7ec4\u6267\u884c\u504f\u79fb\u91cd\u7f6e\u64cd\u4f5c\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\u5728 kafka \u547d\u4ee4\u4e2d\u5c06\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre>kafka-consumer-groups.sh --bootstrap-server $kfk --dry-run --reset-offsets --topic $t --group $cg1 --to-current --export | tee topic-offset.csv<\/pre>\n<pre>kafka-consumer-groups.sh --bootstrap-server $kfk --dry-run --reset-offsets --topic $t --group $cg2 --to-current<\/pre>\n<p>\u7136\u540e\u57fa\u4e8e\u8be5\u5bfc\u51fa\u6587\u4ef6\u5bfc\u5165\u65b0\u7684\u504f\u79fb\u91cf\uff1f<\/p>\n<pre>kafka-consumer-groups.sh --bootstrap-server $kfk --execute --reset-offsets --topic $t --group $cg2 --from-file topic-offset.csv<\/pre>\n<p>\u4ece\u6587\u4ef6\u5bfc\u51fa\u5bfc\u5165\u4e0d\u662f\u95ee\u9898&#8230;\u53ea\u662f\u4f3c\u4e4e\u65e0\u6cd5\u627e\u5230\u83b7\u53d6\u7136\u540e\u8bbe\u7f6e\u504f\u79fb\u91cf\u7684\u65b9\u6cd5..<\/p>\n<p>\u6709\u4eba\u4f7f\u7528 <code>sharma<\/code> \u6216 <code>confluence-kafka-go<\/code> lib \u6765\u73a9\u8fd9\u4e2a\u5417\uff1f<\/p>\n<p>\u611f\u8c22\u60a8\u63d0\u524d\u63d0\u4f9b\u4efb\u4f55\u5efa\u8bae:)<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u662f\u4e00\u4e9b\u6839\u636e ymal \u914d\u7f6e\u8fdb\u884c\u504f\u79fb\u91cd\u7f6e\u7684\u7b80\u5355\u64cd\u4f5c<\/p>\n<\/p>\n<p>\u7528\u4e8e lag \u76d1\u63a7\u6d88\u8d39\u8005\u7ec4\u5217\u8868\u548c\u504f\u79fb\u91cd\u7f6e\u7684\u547d\u4ee4\u884c\u5de5\u5177\u3002<\/p>\n<p>\u6211\u4eec\u6709\u76f8\u540c\u7684\u504f\u79fb\u91cf\u2026\u2026\u73b0\u5728\u4e3a\u4e86\u6a21\u62df\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u6211\u4eec\u5c06\u751f\u6210\u6d88\u606f\u5230\u4e3b\u9898\u5e76\u7ee7\u7eed\u5728\u6d88\u8d39\u8005\u7ec4propertest-cg1a11\u4e4b\u4e00\u4e0a\u6d88\u8d39\uff0c\u6211\u4eec\u5c06\u751f\u62105\u6761\u6d88\u606f\u5e76\u5728\u8be5\u6d88\u8d39\u8005\u7ec4\u4e0a\u6d88\u8d39\u5b83\u4eec\uff0c\u8fd9\u5c06\u4e3a\u6211\u4eec\u63d0\u4f9b\u4ee5\u4e0b\u4fe1\u606f\uff1a\u6211\u4eec\u6d88\u8d39\u4e86<\/p>\n<p><code>^c\u5171\u5904\u7406\u4e8633\u6761\u6d88\u606f<\/code> 28 + 5<\/p>\n<pre> .\/kfkgo lag\nusing config file: ~\/kfk-offsets\/kfk-offset.yaml\ngroup                                    topic                                    partition       current-offset  log-end-offset  lag\npropertest-cg1a11                        propertest                               0               183             183             0\npropertest-cg1a11                        propertest                               1               165             165             0\npropertest-cg1a11                        propertest                               2               192             192             0\npropertest-cg1a11                        propertest                               3               177             177             0\npropertest-cg1a11                        propertest                               4               192             192             0\npropertest-cg1a11                        propertest                               5               169             169             0\npropertest-cg1a11                        propertest                               6               180             180             0\npropertest-cg1a11                        propertest                               7               164             164             0\npropertest-cg1a11                        propertest                               8               195             195             0\npropertest-cg1a11                        propertest                               9               188             188             0\npropertest-cg1a11                        propertest                               10              184             184             0\npropertest-cg1a11                        propertest                               11              184             184             0\ntotal lag:                                                                                                                        0\n\n .\/kfkgo lag -g propertest-cg -t propertest\nusing config file: ~\/kfk-offsets\/kfk-offset.yaml\ngroup                                    topic                                    partition       current-offset  log-end-offset  lag\npropertest-cg                            propertest                               0               179             183             4\npropertest-cg                            propertest                               1               162             165             3\npropertest-cg                            propertest                               2               190             192             2\npropertest-cg                            propertest                               3               174             177             3\npropertest-cg                            propertest                               4               187             192             5\npropertest-cg                            propertest                               5               167             169             2\npropertest-cg                            propertest                               6               177             180             3\npropertest-cg                            propertest                               7               160             164             4\npropertest-cg                            propertest                               8               192             195             3\npropertest-cg                            propertest                               9               185             188             3\npropertest-cg                            propertest                               10              183             184             1\npropertest-cg                            propertest                               11              184             184             0\ntotal lag:                                                                                                                        33<\/pre>\n<p>\u73b0\u5728\u6211\u4eec\u5c06\u504f\u79fb\u91cf\u4ecepropertest-cg\u518d\u6b21\u79fb\u56de\u5230propertest-cg1a11\uff0c\u8fd9\u5c06\u5141\u8bb8\u6211\u4eec\u5728\u8be5cg\u4e0a\u5904\u7406\u76f8\u540c\u7684\u6d88\u606f\u3002<\/p>\n<pre> .\/kfkgo offset -m\nusing config file: ~\/kfk-offsets\/kfk-offset.yaml\nmoving<\/pre>\n<p>\u518d\u6b21\u9a8c\u8bc1\uff1a<\/p>\n<p>kafka \u547d\u4ee4\uff1a<code>kafka-consumer-groups.sh<\/code><\/p>\n<pre>propertest-cg1a11 propertest      0          179             183             4               -               -               -\npropertest-cg1a11 propertest      1          162             165             3               -               -               -\npropertest-cg1a11 propertest      2          190             192             2               -               -               -\npropertest-cg1a11 propertest      3          174             177             3               -               -               -\npropertest-cg1a11 propertest      4          187             192             5               -               -               -\npropertest-cg1a11 propertest      5          167             169             2               -               -               -\npropertest-cg1a11 propertest      6          177             180             3               -               -               -\npropertest-cg1a11 propertest      7          160             164             4               -               -               -\npropertest-cg1a11 propertest      8          192             195             3               -               -               -\npropertest-cg1a11 propertest      9          185             188             3               -               -               -\npropertest-cg1a11 propertest      10         183             184             1               -               -               -\npropertest-cg1a11 propertest      11         184             184             0               -               -               -\n\n\nconsumer group 'propertest-cg' has no active members.\npropertest-cg   propertest      0          179             183             4               -               -               -\npropertest-cg   propertest      1          162             165             3               -               -               -\npropertest-cg   propertest      2          190             192             2               -               -               -\npropertest-cg   propertest      3          174             177             3               -               -               -\npropertest-cg   propertest      4          187             192             5               -               -               -\npropertest-cg   propertest      5          167             169             2               -               -               -\npropertest-cg   propertest      6          177             180             3               -               -               -\npropertest-cg   propertest      7          160             164             4               -               -               -\npropertest-cg   propertest      8          192             195             3               -               -               -\npropertest-cg   propertest      9          185             188             3               -               -               -\npropertest-cg   propertest      10         183             184             1               -               -               -\npropertest-cg   propertest      11         184             184             0               -               -               -\n\n .\/kfkgo lag -g propertest-cg -t propertest\nusing config file: ~\/kfk-offsets\/kfk-offset.yaml\ngroup                                    topic                                    partition       current-offset  log-end-offset  lag\npropertest-cg                            propertest                               0               179             183             4\npropertest-cg                            propertest                               1               162             165             3\npropertest-cg                            propertest                               2               190             192             2\npropertest-cg                            propertest                               3               174             177             3\npropertest-cg                            propertest                               4               187             192             5\npropertest-cg                            propertest                               5               167             169             2\npropertest-cg                            propertest                               6               177             180             3\npropertest-cg                            propertest                               7               160             164             4\npropertest-cg                            propertest                               8               192             195             3\npropertest-cg                            propertest                               9               185             188             3\npropertest-cg                            propertest                               10              183             184             1\npropertest-cg                            propertest                               11              184             184             0\ntotal lag:                                                                                                                        33\n\n .\/kfkgo lag\nusing config file: ~\/kfk-offsets\/kfk-offset.yaml\ngroup                                    topic                                    partition       current-offset  log-end-offset  lag\npropertest-cg1a11                        propertest                               0               179             183             4\npropertest-cg1a11                        propertest                               1               162             165             3\npropertest-cg1a11                        propertest                               2               190             192             2\npropertest-cg1a11                        propertest                               3               174             177             3\npropertest-cg1a11                        propertest                               4               187             192             5\npropertest-cg1a11                        propertest                               5               167             169             2\npropertest-cg1a11                        propertest                               6               177             180             3\npropertest-cg1a11                        propertest                               7               160             164             4\npropertest-cg1a11                        propertest                               8               192             195             3\npropertest-cg1a11                        propertest                               9               185             188             3\npropertest-cg1a11                        propertest                               10              183             184             1\npropertest-cg1a11                        propertest                               11              184             184             0\ntotal lag:                                                                                                                        33<\/pre>\n<p>\u81ea\u8ff0\u6587\u4ef6\u4e2d\u7684\u66f4\u591a\u793a\u4f8b<\/p>\n<p>\u597d\u5427\uff0c\u6211\u60f3\u6211\u53d1\u73b0\u5b83\u53ea\u9700\u8981\u5b9e\u73b0\u5b8c\u6574\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u4f46\u6211\u5e94\u8be5\u64c5\u957f<\/p>\n<pre>func main() {\n    brokers := []string{bk}\n    kfk.logger = log.new(os.stdout, \"\", log.lstdflags)\n\n    cfg := kfk.newconfig()\n    cfg.clientid = cid\n    client, _ := kfk.newclient(brokers, cfg)\n    \/\/fmt.println(client)\n    offsetmg, _ := kfk.newoffsetmanagerfromclient(cg, client)\n    defer offsetmg.close()\n\n    consumer, _ := kfk.newconsumerfromclient(client)\n    defer consumer.close()\n\n    partitions, _ := consumer.partitions(topic)\n\n    for _, p := range partitions {\n\n        pom, _ := offsetmg.managepartition(topic, p)\n        ofs, pomstr := pom.nextoffset()\n        fmt.printf(\"partition: %v -&gt; nextoffset: %v:%s\\n\", p, ofs, pomstr)\n\n    }\n    fmt.println(\"--\")\n\n}<\/pre>\n<p>\u8fd9\u7ed9\u4e86\u6211\u8fd9\u4e2a\u8f93\u51fa\uff1a<\/p>\n<pre>partition: 0 -&gt; nextoffset: 31:\npartition: 1 -&gt; nextoffset: 30:\npartition: 2 -&gt; nextoffset: 45:\npartition: 3 -&gt; nextoffset: 39:\npartition: 4 -&gt; nextoffset: 45:\npartition: 5 -&gt; nextoffset: 39:\npartition: 6 -&gt; nextoffset: 37:\npartition: 7 -&gt; nextoffset: 42:\npartition: 8 -&gt; nextoffset: 43:\npartition: 9 -&gt; nextoffset: 35:\npartition: 10 -&gt; nextoffset: 41:\npartition: 11 -&gt; nextoffset: 36:<\/pre>\n<p>\u4e0e java \u547d\u4ee4\u5b8c\u5168\u76f8\u540c\uff1a<\/p>\n<pre> kafka-consumer-groups.sh --bootstrap-server $kfk --dry-run --reset-offsets --topic $t --group $cg1 --to-current | sort -k3 -n\n\ngroup                          topic                          partition  new-offset\npropertest-cg1                 propertest                     0          31\npropertest-cg1                 propertest                     1          30\npropertest-cg1                 propertest                     2          45\npropertest-cg1                 propertest                     3          39\npropertest-cg1                 propertest                     4          45\npropertest-cg1                 propertest                     5          39\npropertest-cg1                 propertest                     6          37\npropertest-cg1                 propertest                     7          42\npropertest-cg1                 propertest                     8          43\npropertest-cg1                 propertest                     9          35\npropertest-cg1                 propertest                     10         41\npropertest-cg1                 propertest                     11         36<\/pre>\n<p>\u6240\u4ee5\u73b0\u5728\u5269\u4e0b\u7684\u5c31\u662f\u5c06\u8fd9\u4e9b\u6570\u636e\u5bfc\u51fa\u5230\u6587\u4ef6\u5e76\u4f7f\u7528\u51fd\u6570<\/p>\n<pre>\/\/ ResetOffset resets to the provided offset, alongside a metadata string that\n    \/\/ represents the state of the partition consumer at that point in time. Reset\n    \/\/ acts as a counterpart to MarkOffset, the difference being that it allows to\n    \/\/ reset an offset to an earlier or smaller value, where MarkOffset only\n    \/\/ allows incrementing the offset. cf MarkOffset for more details.\n    ResetOffset(topic string, partition int32, offset int64, metadata string)<\/pre>\n<p>\u8bbe\u7f6e\u65b0\u7684\u504f\u79fb\u91cf&#8230;<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aKafka \u6d88\u8d39\u8005\u62b5\u6d88\u5bfc\u51fa golang &#8212; sharma \u6216 confluence-kafka-go lib\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207596","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207596"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207596\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}