|
|
|
@ -0,0 +1,85 @@
|
|
|
|
|
package UnitTest;
|
|
|
|
|
|
|
|
|
|
import org.apache.kafka.clients.consumer.*;
|
|
|
|
|
import org.apache.kafka.common.TopicPartition;
|
|
|
|
|
import org.apache.kafka.common.serialization.StringDeserializer;
|
|
|
|
|
|
|
|
|
|
import java.text.ParseException;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
|
|
public class KafkaTest {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取kafka的时间戳
|
|
|
|
|
*
|
|
|
|
|
* @param dateString
|
|
|
|
|
* @return
|
|
|
|
|
* @throws ParseException
|
|
|
|
|
*/
|
|
|
|
|
public static long getKafkaTimeStamp(String dateString) throws ParseException {
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
Date date = sdf.parse(dateString);
|
|
|
|
|
long millis = date.getTime();
|
|
|
|
|
long kafkaTimestamp = millis / 1000;
|
|
|
|
|
return kafkaTimestamp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws ParseException {
|
|
|
|
|
|
|
|
|
|
Properties props = new Properties();
|
|
|
|
|
props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "10.10.14.67:9092");
|
|
|
|
|
props.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "dsideal-group");
|
|
|
|
|
props.put(ConsumerConfig.CLIENT_ID_CONFIG, "dsideal1"); // 客户端 ID,用于标识消费者
|
|
|
|
|
props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
|
|
|
|
|
props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
|
|
|
|
|
|
|
|
|
|
// 配置消费者客户端时间戳类型为server_time(服务端时间)
|
|
|
|
|
//props.setProperty(ConsumerConfig.TIMESTAMP_TYPE_CONFIG, "server_time");
|
|
|
|
|
|
|
|
|
|
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
// 订阅需要消费的Topic
|
|
|
|
|
consumer.subscribe(Collections.singletonList("test"));
|
|
|
|
|
// 消费消息
|
|
|
|
|
while (true) {
|
|
|
|
|
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
|
|
|
|
|
for (ConsumerRecord<String, String> record : records) {
|
|
|
|
|
String key = record.key();
|
|
|
|
|
String value = record.value();
|
|
|
|
|
long offset = record.offset();
|
|
|
|
|
int partition = record.partition();
|
|
|
|
|
System.out.printf("key=%s,value=%s,offset=%d,partition=%d\n", key, value, offset, partition);
|
|
|
|
|
// 处理收到的消息
|
|
|
|
|
}
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
// 关闭Consumer对象
|
|
|
|
|
//consumer.close();
|
|
|
|
|
|
|
|
|
|
Duration pollTimeout = Duration.ofMillis(100);
|
|
|
|
|
Map<TopicPartition, Long> startOffsets = Collections.singletonMap(new TopicPartition("test", 0), 1651363200000L);
|
|
|
|
|
consumer.assign(startOffsets.keySet());
|
|
|
|
|
Map<TopicPartition, OffsetAndTimestamp> offsetAndTimestamps = consumer.offsetsForTimes(startOffsets);
|
|
|
|
|
for (Map.Entry<TopicPartition, OffsetAndTimestamp> entry : offsetAndTimestamps.entrySet()) {
|
|
|
|
|
TopicPartition tp = entry.getKey();
|
|
|
|
|
OffsetAndTimestamp offsetAndTimestamp = entry.getValue();
|
|
|
|
|
if (offsetAndTimestamp != null) {
|
|
|
|
|
consumer.seek(tp, offsetAndTimestamp.offset());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
ConsumerRecords<String, String> records = consumer.poll(pollTimeout);
|
|
|
|
|
for (ConsumerRecord<String, String> record : records) {
|
|
|
|
|
System.out.printf("topic=%s, partition=%s, offset=%d, timestamp=%s, key=%s, value=%s%n",
|
|
|
|
|
record.topic(), record.partition(), record.offset(), record.timestamp(), record.key(), record.value());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|