要创建一个Kafka消费者组,首先需要确保已经安装并配置了Kafka集群。然后,按照以下步骤创建Kafka消费者组:
properties.put("group.id", "my-consumer-group");
properties.put("bootstrap.servers", "localhost:9092");
consumer.subscribe(Arrays.asList("topic1", "topic2"));
while (true) {
ConsumerRecords<String, String> records = consumer.poll(100);
for (ConsumerRecord<String, String> record : records) {
System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
}
}
通过以上步骤,您就可以成功创建一个Kafka消费者组,并使用消费者组来消费Kafka主题中的消息。