如何按需阅读来自Kafka主题的消息。我使用这三个参数有主题名称,offsetId,PartitionID,如何从Kafka Topic中检索特定消息。可以使用Spring Kafka吗? 我正在使用Spring Boot 2.2.4.RELEASE
答案 0 :(得分:0)
@SpringBootApplication
public class So64759726Application {
public static void main(String[] args) {
SpringApplication.run(So64759726Application.class, args);
}
@Bean
ApplicationRunner runner(ConsumerFactory<String, String> cf) {
return args -> {
try (Consumer<String, String> consumer = cf.createConsumer()) {
TopicPartition tp = new TopicPartition("so64759726", 0);
consumer.assign(Collections.singleton(tp));
consumer.seek(tp, 2);
ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(5));
System.out.println(records.iterator().next().value());
}
};
}
}
application.properties
spring.kafka.consumer.max-poll-records=1