kafka手动修改消费者偏移量

介绍如何使用Kafka的kafka-consumer-groups.sh脚本重置消费者组的位移,包括设置策略和执行方案,并通过实例演示各种位移重置方法。

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

0.11.0.0版本丰富了kafka-consumer-groups脚本的功能,用户可以直接使用该脚本很方便地为已有的consumer group重新设置位移,但前提必须是consumer group必须是inactive的,即不能是处于正在工作中的状态。

  • 确定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:2181 --create --partitions 5 --replication-factor 1 --topic test

Created topic "test".

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

1439666 records sent, 287760.5 records/sec (27.44 MB/sec), 75.7 ms avg latency, 317.0 max latency.
1541123 records sent, 308163.0 records/sec (29.39 MB/sec), 136.4 ms avg latency, 480.0 max latency.
1878025 records sent, 375529.9 records/sec (35.81 MB/sec), 58.2 ms avg latency, 600.0 max latency.
5000000 records sent, 319529.652352 records/sec (30.47 MB/sec), 86.33 ms avg latency, 600.00 ms max latency, 38 ms 50th, 319 ms 95th, 516 ms 99th, 591 ms 99.9th.

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

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

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

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

bogon:kafka_0.11 huxi$ bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --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 0 1000000 1000000 0 consumer-1-8688633a-2f88-4c41-89ca-fd0cd6d19ec7 /127.0.0.1 consumer-1
test 1 1000000 1000000 0 consumer-1-8688633a-2f88-4c41-89ca-fd0cd6d19ec7 /127.0.0.1 consumer-1
test 2 1000000 1000000 0 consumer-1-8688633a-2f88-4c41-89ca-fd0cd6d19ec7 /127.0.0.1 consumer-1
test 3 1000000 1000000 0 consumer-1-8688633a-2f88-4c41-89ca-fd0cd6d19ec7 /127.0.0.1 consumer-1
test 4 1000000 1000000 0 consumer-1-8688633a-2f88-4c41-89ca-fd0cd6d19ec7 /127.0.0.1 consumer-1

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

1. --to-earliest

bogon:kafka_0.11 huxi$ bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --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 0 0 
test 1 0 
test 4 0 
test 3 0 
test 2 0

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

2. --to-latest

bogon:kafka_0.11 huxi$ bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --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 0 1000000 
test 1 1000000 
test 4 1000000 
test 3 1000000 
test 2 1000000

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

3.  --to-offset <offset>

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

TOPIC PARTITION NEW-OFFSET 
test 0 500000 
test 1 500000 
test 4 500000 
test 3 500000 
test 2 500000

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

4.  --to-current

bogon:kafka_0.11 huxi$ bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --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 0 500000 
test 1 500000 
test 4 500000 
test 3 500000 
test 2 500000

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

5. --shift-by N

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

TOPIC PARTITION NEW-OFFSET 
test 0 400000 
test 1 400000 
test 4 400000 
test 3 400000 
test 2 400000

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

6. --to-datetime

bogon:kafka_0.11 huxi$ bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group test-group --reset-offsets --all-topics --to-datetime 2017-08-04T14:30: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 0 1000000 
test 1 1000000 
test 4 1000000 
test 3 1000000 
test 2 1000000

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

7. --by-duration

bogon:kafka_0.11 huxi$ bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --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 0 0 
test 1 0 
test 4 0 
test 3 0 
test 2 0

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

在 Apache Kafka 中,消费者偏移量(Offset)是消费者读取日志位置的指针。消费者可以通过不同的方式调整或修改偏移量,以便实现从特定位置重新消费消息的需求。以下是几种常见的修改 Kafka 消费者偏移量的方法及其使用场景。 ### 1. 自动提交偏移量 Kafka 消费者可以配置为自动提交偏移量。通过设置 `enable.auto.commit` 为 `true`,消费者会在后台周期性地提交偏移量,通常默认间隔为 5 秒。这种机制简化了偏移量管理,但可能会导致消息重复消费或丢失,具体取决于业务逻辑的容错能力。 ```java Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("group.id", "test-group"); props.put("enable.auto.commit", "true"); props.put("auto.commit.interval.ms", "5000"); props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); ``` 自动提交适用于对消息处理的精确性要求不高的场景[^3]。 ### 2. 手动提交偏移量 如果需要更精细地控制偏移量的提交,可以将 `enable.auto.commit` 设置为 `false`,并通过调用 `commitSync()` 或 `commitAsync()` 方法手动提交偏移量。这种方式确保消息被完全处理后再提交偏移量,从而避免消息丢失。 ```java props.put("enable.auto.commit", "false"); // 在处理完消息后手动提交 consumer.commitSync(); ``` 手动提交提供了更高的可靠性,但增加了代码复杂性,适用于对数据一致性要求较高的场景[^3]。 ### 3. 通过命令行工具重置偏移量 Kafka 提供了命令行工具来重置消费者偏移量。例如,可以将偏移量设置为最早(earliest)或最新(latest)位置。 ```bash bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group test-group --reset-offsets --to-earliest --execute --topic test-topic ``` 此命令会将指定消费者 `test-group` 的所有分区偏移量重置为最早的位置。同样,可以通过 `--to-latest` 参数重置为最新的偏移量[^2]。 ### 4. 使用 Kafka 工具类修改偏移量 Kafka 提供了 `kafka.tools.UpdateOffsetsInZK` 工具类来修改消费者偏移量。该工具类需要通过 ZooKeeper 操作,适用于 Kafka 旧版本(依赖 ZooKeeper 管理消费者元数据的版本)。 ```bash bin/kafka-run-class.sh kafka.tools.UpdateOffsetsInZK earliest --zkconnect=localhost:2181 --group=test-group ``` 此命令会将指定消费者偏移量设置为最早位置。`earliest` 和 `latest` 是两个常用选项,分别表示从最早或最新的偏移量开始消费[^2]。 ### 5. 自定义偏移量存储 在某些高级场景中,开发者可以选择将偏移量存储在外部系统中(如数据库、Redis 等),并在消费者启动时从这些系统中读取偏移量。这种方式提供了更高的灵活性,但也增加了系统复杂性和运维成本。 ```java // 示例:从外部存储读取偏移量 long storedOffset = getOffsetFromExternalStorage(); consumer.seek(new TopicPartition("topic", 0), storedOffset); ``` 通过 `seek()` 方法可以将消费者定位到特定的偏移量位置。适用于需要实现复杂的偏移量管理逻辑的场景[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值