2018-06-04 kafka从某个时间节点开始消费(单个消费者)

##通过遍历该主题下所有的分区,获得从某个时间节点开始的数据(单个消费者),这种方法取出的数据是无序的,要想取出的数据连续需要很好的分区策略,最好是连续的数据能落到同一个分区

Properties props = new Properties();
props.put("bootstrap.servers", brokerList);
props.put("group.id", "group2");
//    	props.put("auto.commit.enable", "false");
//    	props.put("auto.commit.interval.ms", "1000"); //设置enable.auto.commit意味着偏移是通过config auto.commit.interval.ms控制的频率自动提交的。
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

KafkaConsumer consumer = new KafkaConsumer<>(props);
//    	consumer.subscribe(Arrays.asList(topic));//过滤话题
String startTime = "2018-05-31 16:25:00";//起始时间点
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Long stime = sdf.parse(startTime).getTime();
//存放主题分区信息
List partitionSet = consumer.partitionsFor(topic);
Map offsetMapAll  = new HashMap();
for (PartitionInfo partitionInfo : partitionSet) {
	//存放主题信息和时间戳
	Map startmap = new HashMap();
	TopicPartition topicPartition = new TopicPartition(partitionInfo.topic(), partitionInfo.partition());
	startmap.put(topicPartition, stime);
	//给consumer指定分区
	consumer.assign(Arrays.asList(topicPartition));
	//获得该分区的起始偏移量
	Map offsetMap = consumer.offsetsForTimes(startmap);
	if(offsetMap.get(topicPartition)==null) {
		continue;
	}
	offsetMapAll.put(topicPartition, offsetMap.get(topicPartition).offset());
}
if(offsetMapAll.size()>0) {
	consumer.assign(offsetMapAll.keySet());
	for(TopicPartition tp : offsetMapAll.keySet()){
		consumer.seek(tp, offsetMapAll.get(tp));//设置分区的起始偏移量
	}
	int cnt = 0;
	//轮询读取消息
	while (true) {
		
		ConsumerRecords records = consumer.poll(1000);
		
		for (ConsumerRecord record : records){
//        			TimeUnit.SECONDS.sleep(1);
			System.out.printf("offset = %d, key = %s, partition = %s%n", record.offset(), record.key(), record.partition());
			logger.error( record.offset()+"\t"+ record.key()+"\t"+record.partition());
			cnt ++;
		}
		System.out.println("总数:" + cnt);
	}
}

你可能感兴趣的:(学习)