|
|
|
@ -78,9 +78,21 @@ public class kafkaConsumerTest {
|
|
|
|
|
//从指定时间点开始消费
|
|
|
|
|
List<PartitionInfo> topicPartitions = consumer.partitionsFor(TOPIC_NAME);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
//从1小时前开始消费
|
|
|
|
|
long fetchDataTime = new Date().getTime() - 1000 * 60 * 60;
|
|
|
|
|
// long fetchDataTime = new Date().getTime() - 1000 * 60 * 60;
|
|
|
|
|
|
|
|
|
|
//从某个时间点获取之后的数据
|
|
|
|
|
String dateString = "2023-05-10 08:38:00";
|
|
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
Date startDate = null;
|
|
|
|
|
try {
|
|
|
|
|
startDate = dateFormat.parse(dateString);
|
|
|
|
|
} catch (ParseException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long fetchDataTime = startDate.getTime();
|
|
|
|
|
Map<TopicPartition, Long> map = new HashMap<>();
|
|
|
|
|
for (PartitionInfo par : topicPartitions) {
|
|
|
|
|
map.put(new TopicPartition(TOPIC_NAME, par.partition()), fetchDataTime);
|
|
|
|
@ -99,7 +111,7 @@ public class kafkaConsumerTest {
|
|
|
|
|
consumer.seek(key, offset);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
/*
|
|
|
|
@ -107,8 +119,8 @@ public class kafkaConsumerTest {
|
|
|
|
|
*/
|
|
|
|
|
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
|
|
|
|
|
for (ConsumerRecord<String, String> record : records) {
|
|
|
|
|
System.out.printf("收到消息:partition = %d,offset = %d, key = %s, value = %s%n", record.partition(),
|
|
|
|
|
record.offset(), record.key(), record.value());
|
|
|
|
|
System.out.printf("收到消息:partition = %d,offset = %d, key = %s, value = %s ,timestamp = %d%n", record.partition(),
|
|
|
|
|
record.offset(), record.key(), record.value(), record.timestamp());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (records.count() > 0) {
|
|
|
|
|