转载自 huxihx,原文链接 Kafka consumer group位移重设

本文阐述如何使用Kafka自带的kafka-consumer-groups.sh脚本随意设置消费者组(consumer group)的位移。需要特别强调的是, 这是0.11.0.0版本提供的新功能且只适用于新版本consumer。

在新版本之前,如果要为已有的consumer group调整位移必须要手动编写Java程序调用KafkaConsumer#seek方法,费时费力不说还容易出错。0.11.0.0版本丰富了kafka-consumer-groups脚本的功能,用户可以直接使用该脚本很方便地为已有的consumer group重新设置位移,但前提是:consumer group状态必须是inactive的,即不能是处于正在工作中的状态。

先务虚一下。总体来说,重设位移的流程由3步组成,如下图所示:

  • 确定topic作用域——当前有3种作用域指定方式:--all-topics(为consumer group下所有topic的所有分区调整位移),--topic t1 --topic t2(为指定的若干个topic的所有分区调整位移),--topic t1:0,1,2(为指定的topic分区调整位移)
  • 确定位移重设策略——当前支持8种设置规则:
    • --to-earliest:把位移调整到分区当前最小位移
    • --to-latest:把位移调整到分区当前最新位移
    • --to-current:把位移调整到分区当前位移
    • --to-offset <offset>: 把位移调整到指定位移处
    • --shift-by N: 把位移调整到当前位移 + N处,注意N可以是负数,表示向前移动
    • --to-datetime <datetime>:把位移调整到大于给定时间的最早位移处,datetime格式是yyyy-MM-ddTHH:mm:ss.xxx,比如2017-08-04T00:00:00.000
    • --by-duration <duration>:把位移调整到距离当前时间指定间隔的位移处,duration格式是PnDTnHnMnS,比如PT0H5M0S
    • --from-file <file>:从CSV文件中读取调整策略
  • 确定执行方案——当前支持3种方案:
    • 什么参数都不加:只是打印出位移调整方案,不具体执行
    • --execute:执行真正的位移调整
    • --export:把位移调整方案按照CSV格式打印,方便用户成csv文件,供后续直接使用

针对上面的8种策略,本文重点演示前面7种策略。

首先,我们创建一个测试topic,5个分区,并发送5,000,000条测试消息:

> bin/kafka-topics.sh --zookeeper localhost: --create --partitions  --replication-factor  --topic test

Created topic "test".

> bin/kafka-producer-perf-test.sh --topic test --num-records  --throughput - --record-size  --producer-props bootstrap.servers=localhost: acks=-

 records sent, 287760.5 records/sec (27.44 MB/sec), 75.7 ms avg latency, 317.0 max latency.
records sent, 308163.0 records/sec (29.39 MB/sec), 136.4 ms avg latency, 480.0 max latency.
records sent, 375529.9 records/sec (35.81 MB/sec), 58.2 ms avg latency, 600.0 max latency.
records sent, 319529.652352 records/sec (30.47 MB/sec), 86.33 ms avg latency, 600.00 ms max latency, ms 50th, ms 95th, ms 99th, ms .9th.

然后,启动一个console consumer程序,组名设置为test-group:

bin/kafka-console-consumer.sh --bootstrap-server localhost: --topic test --from-beginning --consumer-property group.id=test-group

..............

待运行一段时间后关闭consumer程序将group设置为inactive。现在运行kafka-consumer-groups.sh脚本首先确定当前group的消费进度:

bogon:kafka_0. huxi$ bin/kafka-consumer-groups.sh --bootstrap-server localhost: --group test-group --describe
Note: This will only show information about consumers that use the Java consumer API (non-ZooKeeper-based consumers). TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
test consumer--8688633a-2f88-4c41-89ca-fd0cd6d19ec7 /127.0.0.1 consumer-
test consumer--8688633a-2f88-4c41-89ca-fd0cd6d19ec7 /127.0.0.1 consumer-
test consumer--8688633a-2f88-4c41-89ca-fd0cd6d19ec7 /127.0.0.1 consumer-
test consumer--8688633a-2f88-4c41-89ca-fd0cd6d19ec7 /127.0.0.1 consumer-
test consumer--8688633a-2f88-4c41-89ca-fd0cd6d19ec7 /127.0.0.1 consumer-

由上面输出可知,当前5个分区LAG列的值都是0,表示全部消费完毕。现在我们演示下如何重设位移。

1. --to-earliest

bogon:kafka_0. huxi$ bin/kafka-consumer-groups.sh --bootstrap-server localhost: --group test-group --reset-offsets --all-topics --to-earliest --execute
Note: This will only show information about consumers that use the Java consumer API (non-ZooKeeper-based consumers). TOPIC PARTITION NEW-OFFSET
test
test
test
test
test

上面输出表明,所有分区的位移都已经被重设为0

2. --to-latest

bogon:kafka_0. huxi$ bin/kafka-consumer-groups.sh --bootstrap-server localhost: --group test-group --reset-offsets --all-topics --to-latest --execute
Note: This will only show information about consumers that use the Java consumer API (non-ZooKeeper-based consumers). TOPIC PARTITION NEW-OFFSET
test
test
test
test
test

上面输出表明,所有分区的位移都已经被重设为最新位移,即1,000,000

3.  --to-offset <offset>

bogon:kafka_0. huxi$ bin/kafka-consumer-groups.sh --bootstrap-server localhost: --group test-group --reset-offsets --all-topics --to-offset  --execute
Note: This will only show information about consumers that use the Java consumer API (non-ZooKeeper-based consumers). TOPIC PARTITION NEW-OFFSET
test
test
test
test
test

上面输出表明,所有分区的位移都已经调整为给定的500000

4.  --to-current

bogon:kafka_0. huxi$ bin/kafka-consumer-groups.sh --bootstrap-server localhost: --group test-group --reset-offsets --all-topics --to-current --execute
Note: This will only show information about consumers that use the Java consumer API (non-ZooKeeper-based consumers). TOPIC PARTITION NEW-OFFSET
test
test
test
test
test

输出表明所有分区的位移都已经被移动到当前位移(这个有点傻,因为位移距上一步没有变动)

5. --shift-by N

bogon:kafka_0. huxi$ bin/kafka-consumer-groups.sh --bootstrap-server localhost: --group test-group --reset-offsets --all-topics --shift-by - --execute
Note: This will only show information about consumers that use the Java consumer API (non-ZooKeeper-based consumers). TOPIC PARTITION NEW-OFFSET
test
test
test
test
test

输出表明所有分区的位移被移动到(500000 - 100000) = 400000处

6. --to-datetime

bogon:kafka_0. huxi$ bin/kafka-consumer-groups.sh --bootstrap-server localhost: --group test-group --reset-offsets --all-topics --to-datetime --04T14::00.000
Note: This will only show information about consumers that use the Java consumer API (non-ZooKeeper-based consumers). TOPIC PARTITION NEW-OFFSET
test
test
test
test
test

将所有分区的位移调整为2017年8月4日14:30之后的最早位移

7. --by-duration

bogon:kafka_0. huxi$ bin/kafka-consumer-groups.sh --bootstrap-server localhost: --group test-group --reset-offsets --all-topics --by-duration PT0H30M0S
Note: This will only show information about consumers that use the Java consumer API (non-ZooKeeper-based consumers). TOPIC PARTITION NEW-OFFSET
test
test
test
test
test

将所有分区位移调整为30分钟之前的最早位移。

Kafka设计解析(十九)Kafka consumer group位移重设的更多相关文章

  1. Kafka consumer group位移重设

    本文阐述如何使用Kafka自带的kafka-consumer-groups.sh脚本随意设置消费者组(consumer group)的位移.需要特别强调的是, 这是0.11.0.0版本提供的新功能且只 ...

  2. Kafka设计解析(九)为何去掉replica.lag.max.messages参数

    转载自 huxihx,原文链接 Kafka副本管理—— 为何去掉replica.lag.max.messages参数 在Kafka设计解析(二)Kafka High Availability (上)文 ...

  3. Kafka consumer group位移0ffset重设

    本文阐述如何使用Kafka自带的kafka-consumer-groups.sh脚本随意设置消费者组(consumer group)的位移.需要特别强调的是, 这是0.11.0.0版本提供的新功能且只 ...

  4. Kafka设计解析(十三)Kafka消费组(consumer group)

    转载自 huxihx,原文链接 Kafka消费组(consumer group) 一直以来都想写一点关于kafka consumer的东西,特别是关于新版consumer的中文资料很少.最近Kafka ...

  5. Kafka设计解析(四)Kafka Consumer设计解析

    转载自 技术世界,原文链接 Kafka设计解析(四)- Kafka Consumer设计解析 目录 一.High Level Consumer 1. Consumer Group 2. High Le ...

  6. Kafka设计解析(十二)Kafka 如何读取offset topic内容 (__consumer_offsets)

    转载自 huxihx,原文链接 Kafka 如何读取offset topic内容 (__consumer_offsets) 众所周知,由于Zookeeper并不适合大批量的频繁写入操作,新版Kafka ...

  7. Kafka设计解析(五)- Kafka性能测试方法及Benchmark报告

    本文转发自Jason’s Blog,原文链接 http://www.jasongj.com/2015/12/31/KafkaColumn5_kafka_benchmark 摘要 本文主要介绍了如何利用 ...

  8. 揭秘Kafka高性能架构之道 - Kafka设计解析(六)

    原创文章,同步首发自作者个人博客.转载请务必在文章开头处以超链接形式注明出处http://www.jasongj.com/kafka/high_throughput/ 摘要 上一篇文章<Kafk ...

  9. 流式处理的新贵 Kafka Stream - Kafka设计解析(七)

    原创文章,转载请务必将下面这段话置于文章开头处. 本文转发自技术世界,原文链接 http://www.jasongj.com/kafka/kafka_stream/ Kafka Stream背景 Ka ...

随机推荐

  1. VS窗体换肤

    1.首先我们要下载一个皮肤 vs窗体皮肤下载官网:http://irisskin.software.informer.com/download/ 2.创建一个lib文件夹 把下载好的引用放到里面 3. ...

  2. vue Element-UI 分页使用(1)

    最近在使用Element-UI这套框架配合Vue来写前端页面.在用Element-UI来制作表格的时候,遇到了一些小问题,记录方便学习. 其中两个事件是关于切换当前页和切换显示的列表条数的.另外的属性 ...

  3. 高性能JavaScript(数据存取)

    数据存取分为4各部分 存取位置 作用域及改变作用域 原型以及原型链 缓存对象成员值 存取位置 JavaScript 有4中基本的数据存取位置 字面量:字面量代表自身,不存于特定的位置.比如这个的匿名函 ...

  4. 高德地图 JS API - 根据经纬度获取周边建筑地标

    像我们经常用的微信或微博,发表动态时都有选择位置的功能,根据当前的定位获取附近的地标.利用高德地图我们就可以实现这样的功能. 1. 具体代码: // 高德地图查询周边 function aMapSea ...

  5. JSP内置对象——response对象

    看一个实例: 运行结果: 出现了一个很奇怪的现象,这个outer对象输出的字符串,跑到顶部去了.这个呢也就说明了response对象获得的writer对象的输出总是前于我们的内置对象.(respons ...

  6. EditText的焦点问题

    问题说明: activity中有个三级菜单,三个ListView嵌套,最后一层ListView的item中有EditText控件.要求EditText不仅能手动输入,还能点击加减进行改变.EditTe ...

  7. Android--用Valley框架去上传图片

    1.除了用到了Volley,还用到了一个包httpmime(下载地址:http://download.csdn.net/detail/chequer_lkp/8102751) 2.需要一个工具类,该类 ...

  8. [WPF 基础知识系列] —— 绑定中的数据校验Vaildation

    前言: 只要是有表单存在,那么就有可能有对数据的校验需求.如:判断是否为整数.判断电子邮件格式等等. WPF采用一种全新的方式 - Binding,来实现前台显示与后台数据进行交互,当然数据校验方式也 ...

  9. LeetCode题解之 Increasing Order Search Tree

    1.题目描述 2/问题分析 利用中序遍历,然后重新构造树. 3.代码 TreeNode* increasingBST(TreeNode* root) { if (root == NULL) retur ...

  10. 调研getfit

    Gitfit实际是一个提供私人教练的服务,其主要业务有三种,“局部减脂”每天0.5-1小时,对局部高强度的刺激,快速达到塑形目地,不需要复杂器械,0基础也能跟上训练进度,并提供咨询师.营养师团队.专属 ...