如何使用Java中Kafka的消息,从特定偏移量开始
从早到晚阅读:如何使用Java中Kafka的消息,从特定偏移量开始,java,apache-kafka,kafka-consumer-api,Java,Apache Kafka,Kafka Consumer Api,从早到晚阅读: props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); 阅读最新消息: props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest"); 但是,如果我想开始,我应该使用哪一行?例如:从第18个提交开始?您可以使用该行强制消费者从特定偏移开始消费: 公共无效搜索(TopicPartition分区,长偏移量) 覆盖使用者将在下一次轮询(超时)时使用的提
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
阅读最新消息:
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");
但是,如果我想开始,我应该使用哪一行?例如:从第18个提交开始?您可以使用该行强制消费者从特定偏移开始消费:
公共无效搜索(TopicPartition分区,长偏移量)
覆盖使用者将在下一次轮询(超时)
时使用的提取偏移量。如果为
同一分区不止一次,最新的偏移量将用于
下一步poll()
。请注意,如果随意使用此API,可能会丢失数据
用于消费中值,重置取差额
例如,假设您希望从偏移量
18
开始:
TopicPartition tp = new TopicPartition("myTopic", 0);
Long startOffset = 18L
List<TopicPartition> topics = Arrays.asList(tp);
consumer.assign(topics);
consumer.seek(topicPartition, startOffset);
// Then consume messages as normal..
TopicPartition tp=新的TopicPartition(“myTopic”,0);
长STARTOFSET=18L
列表主题=Arrays.asList(tp);
消费者。分配(主题);
consumer.seek(主题分区,startOffset);
//然后像平常一样使用消息。。