Kafka : 基于日志结构(log-based)的消息引擎
重设位移的两个维度 :
7 种重设策略 :
维度 | 策略 | 含义 |
---|---|---|
位移维度 | Earliest | 位移调整当前最早位移处 |
Latest | 位移调整到当前最新位移处 | |
Current | 位移调整到当前最新提交位移处 | |
Specified-Offset | 位移调整成指定位移 | |
Shift-By-N | 位移调整到当前位移 + N 处 | |
时间维度 | DataTime | 位移调整到大于给定时间的最小位移处 |
Duration | 位移调整到离当前时间指定间隔的位移处 |
Earliest 策略 : 将位移调整到主题当前最早位移处
Latest 策略 : 把位移重设成最新末端位移
Current 策略 : 将位移调整成消费者当前提交的最新位移
Specified-Offset 策略 : 消费者把位移值调整到你指定的位移处
Specified-Offset 策略 : 指定位移的绝对数值
Shift-By-N 策略 : 指定位移的相对数值 : 跳过一段消息的距离
DateTime : 指定一个时间,将位移重置到该时间之后的最早位移处
Duration 策略 : 相对的时间间隔,将位移调整到距离当前给定时间间隔的位移处,格式是 PnDTnHnMnS
PT0H15M0S
Earliest 策略 :
Properties consumerProperties = new Properties();
// 禁止自动提交位移
consumerProperties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
// 重设的消费者组的组 ID
consumerProperties.put(ConsumerConfig.GROUP_ID_CONFIG, groupID);
consumerProperties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
consumerProperties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
consumerProperties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
consumerProperties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerList);String topic = "test"; // 要重设位移的 Kafka 主题
try (final KafkaConsumer consumer = new KafkaConsumer<>(consumerProperties)) {consumer.subscribe(Collections.singleton(topic));consumer.poll(0);// 一次性构造主题的所有分区对象consumer.seekToBeginning( consumer.partitionsFor(topic).stream().map(partitionInfo ->new TopicPartition(topic, partitionInfo.partition())).collect(Collectors.toList()));
}
Latest 策略 :
consumer.seekToEnd(consumer.partitionsFor(topic).stream().map(partitionInfo -> new TopicPartition(topic, partitionInfo.partition())).collect(Collectors.toList()));
Current 策略 : 获取当前提交的最新位移 :
consumer.partitionsFor(topic).stream().map(info -> new TopicPartition(topic, info.partition())).forEach(tp -> {long committedOffset = consumer.committed(tp).offset();consumer.seek(tp, committedOffset);
});
Specified-Offset 策略 :
long targetOffset = 1234L;for (PartitionInfo info : consumer.partitionsFor(topic)) {TopicPartition tp = new TopicPartition(topic, info.partition());consumer.seek(tp, targetOffset);
}
实现 Shift-By-N 策略
for (PartitionInfo info : consumer.partitionsFor(topic)) {TopicPartition tp = new TopicPartition(topic, info.partition());// 假设向前跳 123 条消息long targetOffset = consumer.committed(tp).offset() + 123L; consumer.seek(tp, targetOffset);
}
DateTime 策略 : 重设位移到 2022 年 12 月 11 日晚上 8 点
long ts = LocalDateTime.of(2022, 12, 11, 20, 0).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();Map timeToSearch = consumer.partitionsFor(topic).stream().map(info -> new TopicPartition(topic, info.partition())).collect(Collectors.toMap(Function.identity(), tp -> ts));for (Map.Entry entry : consumer.offsetsForTimes(timeToSearch).entrySet()) {consumer.seek(entry.getKey(), entry.getValue().offset());
}
Duration 策略 : 将位移调回 30 分钟前
Map timeToSearch = consumer.partitionsFor(topic).stream().map(info -> new TopicPartition(topic, info.partition())).collect(Collectors.toMap(Function.identity(), tp -> System.currentTimeMillis() - 30 * 1000 * 60));for (Map.Entry entry : consumer.offsetsForTimes(timeToSearch).entrySet()) {consumer.seek(entry.getKey(), entry.getValue().offset());
}
Earliest 策略 :
bin/kafka-consumer-groups.sh \
--bootstrap-server kafka-host:port \
--group test-group --reset-offsets \
--all-topics --to-earliest –execute
Latest 策略 :
bin/kafka-consumer-groups.sh \
--bootstrap-server kafka-host:port \
--group test-group --reset-offsets \
--all-topics --to-latest --execute
Current 策略 :
bin/kafka-consumer-groups.sh \
--bootstrap-server kafka-host:port \
--group test-group --reset-offsets \
--all-topics --to-current --execute
Specified-Offset 策略 :
bin/kafka-consumer-groups.sh \
--bootstrap-server kafka-host:port \
--group test-group --reset-offsets \
--all-topics --to-offset --execute
Shift-By-N 策略 :
bin/kafka-consumer-groups.sh \
--bootstrap-server kafka-host:port \
--group test-group --reset-offsets \
--shift-by --execute
DateTime 策略 :
bin/kafka-consumer-groups.sh \
--bootstrap-server kafka-host:port \
--group test-group --reset-offsets \
--to-datetime 2019-06-20T20:00:00.000 --execute
Duration 策略 :
bin/kafka-consumer-groups.sh \
--bootstrap-server kafka-host:port \
--group test-group --reset-offsets \
--by-duration PT0H30M0S --execute
上一篇:pyqt5环境搭建
下一篇:Makefile的概述