我一直致力于Kafka流编码。首先尝试创建一些样本来理解。
我尝试使用以下Consumer
代码订阅一个主题,但它运行正常。
try {
while (true) {
consumer.subscribe(Collections.singletonList(this.topic));
ConsumerRecords<String, String> records = consumer.poll(1000);
for (ConsumerRecord<String, String> record : records) {
// To Retrieve Data in this Consumer
System.out.println("Received message: " + record.value() );
}
}
} finally {
consumer.close();
}
当我想要使用多个主题时,我尝试了以下示例,
try {
while (true) {
consumer.subscribe(Arrays.asList(topic, topic1));
ConsumerRecords<String, String> records = consumer.poll(1000);
for (ConsumerRecord<String, String> record : records) {
// To Retrieve Data in this Consumer
System.out.println("Received message: " + record.value() );
}
}
} finally {
consumer.close();
}
但这只是从一个主题接收记录,而不是来自两个主题。有人可以指导我如何使用多个主题的记录?