【问题标题】:Consume messages from Kafka topic partition from an offset / from a date when auto commit set to true从偏移量/从自动提交设置为 true 的日期使用来自 Kafka 主题分区的消息
【发布时间】:2022-08-15 22:19:03
【问题描述】:

使用的依赖阿尔帕卡卡夫卡 3.0

我们有以下消费者设置。

enable.auto.commit = true

auto.offset.reset = 最早

如果我们有 enable.auto.commit = true 那么是否可以从特定偏移量/某个日期消费来自 Kafka 主题分区的消息?

    标签: apache-kafka akka kafka-consumer-api alpakka


    【解决方案1】:

    使用 assing 方法而不是 subscribe 方法

     public void sample() {
            KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(properties);
            TopicPartition partition = new TopicPartition("some-topic", 0);
            consumer.assign(Arrays.asList(partition));
            consumer.seek(partition, 0);
            while (true) {
                final ConsumerRecords<Long, String> consumerRecords = consumer.poll(1000);
            }
        }
    

    据我所知,当您使用assign方法而不是subscribe方法时,不需要指定消费者组,在 Consumer 属性,因此可能会忽略这些参数(enable.auto.commit、auto.offset.reset)。使用 assign 方法并仅用于调试或测试目的。

    还有另一种称为 offsetsForTimes 的方法来获取要查找的所需偏移量。

    【讨论】:

    • 在 Alpakka Kafka 中,您应该能够使用 Alpakka 的 Consumer 中的 plainExternalSource 获得类似的结果。
    • 消费者偏移量仅存储在 Kafka 中。没有其他数据源用于存储偏移量。是否可以使用 plainExternalSource API 从偏移量或特定时间戳中检索记录?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 2019-07-20
    • 2021-05-04
    • 2015-02-14
    • 1970-01-01
    相关资源
    最近更新 更多