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,
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;
}