kafka设置起止时间消费消息

消费kafka消息时,有时可能需要消费某个时间段的消息,写个demo记录下:

public class KafkaConsumerByTime {

    public static void main(String[] args) throws Exception {

        String topic = "start_log";
        String startTime = "2019-11-07 10:26:00";
        String endTime = "2019-11-07 10:28:00";
        Properties  kafkaProp = new Properties();
        kafkaProp.put("bootstrap.servers", "localhost:9092");
        kafkaProp.put("group.id", "testByTime");
        kafkaProp.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        kafkaProp.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

        KafkaConsumerByTime task = new KafkaConsumerByTime(topic, kafkaProp, startTime, endTime);
        task.doTask();


    }

    private static final String PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * Pattern:yyyy-MM-dd HH:mm:ss
     */
    private String startTime;

    /**
     * Pattern:yyyy-MM-dd HH:mm:ss
     */
    private String endTime;

    /**
     * kafka配置
     */
    private Properties kafkaProp;

    private String topic;

    private boolean isCancel = false;


    public KafkaConsumerByTime(String topic, Properties kafkaProp, String startTime, String endTime) {
        this.topic = topic;
        this.kafkaProp = kafkaProp;
        this.startTime = startTime;
        this.endTime = endTime;
    }

    public void doTask() throws Exception{

        long sTime = DateUtils.parseDate(startTime, PATTERN).getTime();
        long eTime =  DateUtils.parseDate(endTime, PATTERN).getTime();

        Map<TopicPartition, OffsetAndTimestamp> startOffsetMap = KafkaUtil.fetchOffsetsWithTimestamp(topic, sTime, kafkaProp);

        try (KafkaConsumer<String, String> consumer = new KafkaConsumer<>(kafkaProp)) {
            consumer.assign(startOffsetMap.keySet());

            startOffsetMap.forEach((k, v) -> {
                consumer.seek(k, v.offset());
                System.out.println(topic + ":partition:" + k + ", offsets:" + v.offset());
            });
            System.out.println("开始消费");
            while(!isCancel) {
                System.out.println("循环...");
                ConsumerRecords<String, String> records = consumer.poll(1000);
                for (ConsumerRecord<String, String> record : records) {
                    if (eTime == 0 || record.timestamp() <= eTime) {
                        // doSomething
                        System.out.printf("offset = %d,p = %d, timestamp = %d key = %s, value = %s \r\n", record.offset(), record.partition(), record.timestamp(), record.key(), record.value());
                    }
                }
            }
            System.out.println("结束消费");
        }catch (Exception e) {
           e.printStackTrace();
        }
        
    }

    public void setCancel(boolean cancel) {
        isCancel = cancel;
    }
}

你可能感兴趣的:(kafka)