In Kafka how to get the exact offset according producing time

后端 未结 5 2120
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-04 07:58

I need to get the message produced in Kafka hour by hour in a day. Every one hour I will launch a job to consume the message produced 1 hour ago. e.g., if current time is 20:12,

5条回答
  •  渐次进展
    2021-02-04 08:15

    Show you the code:

    public static Map getOffsetAndTimestampAtTime(String kafkaServer, String topic, long time) {
        Map kafkaParams = new HashMap<>();
        kafkaParams.put(BOOTSTRAP_SERVERS_CONFIG, kafkaServers);
        kafkaParams.put(GROUP_ID_CONFIG, "consumerGroupId");
        kafkaParams.put(KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        kafkaParams.put(VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        kafkaParams.put(AUTO_OFFSET_RESET_CONFIG, "latest");
        kafkaParams.put(ENABLE_AUTO_COMMIT_CONFIG, false);
        KafkaConsumer consumer = new KafkaConsumer<>(kafkaParams);
    
        List partitionInfos = consumer.partitionsFor(topic);
    
        List topicPartitions = partitionInfos
                .stream()
                .map(pi -> new TopicPartition(pi.topic(), pi.partition()))
                .collect(Collectors.toList());
    
        Map topicPartitionToTimestampMap = topicPartitions.stream()
                .collect(Collectors.toMap(tp -> tp, tp -> time));
    
        Map result = consumer.offsetsForTimes(topicPartitionToTimestampMap);
        consumer.close();
        return result;
    }
    

提交回复
热议问题