如何实现"java消费kafka多个topic"

1. 整体流程

步骤 描述
1 创建Kafka消费者实例
2 订阅多个topic
3 消费消息
gantt
    title 实现"java消费kafka多个topic"流程图
    section 创建Kafka消费者实例
    创建Kafka消费者实例            :done, a1, 2022-01-01, 2d
    section 订阅多个topic
    订阅多个topic            :done, a2, after a1, 1d
    section 消费消息
    消费消息            :done, a3, after a2, 2d

2. 具体步骤

步骤1:创建Kafka消费者实例

// 创建Kafka消费者实例
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "test-group");
props.put("enable.auto.commit", "true");
props.put("auto.commit.interval.ms", "1000");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

// 创建消费者
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);

步骤2:订阅多个topic

// 订阅多个topic
consumer.subscribe(Arrays.asList("topic1", "topic2", "topic3"));

步骤3:消费消息

try {
    while (true) {
        ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
        for (ConsumerRecord<String, String> record : records) {
            System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
        }
    }
} finally {
    consumer.close();
}

结论

通过以上步骤,你已经学会了如何用Java消费Kafka多个topic的消息。首先,你需要创建Kafka消费者实例,然后订阅多个topic,最后消费消息。记得根据自己的实际情况调整代码中的配置参数和topic名称。祝你在Kafka的学习过程中顺利!