ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
* 首先确定一点,我使用的kafka是2.11-2.11版本。kafka java API已经发生了变化。 所以首先明确,要去看文档![消费者javaAPI](http://kafka.apache.org/23/javadoc/index.html?org/apache/kafka/clients/consumer/KafkaConsumer.html) ~~~java package com.bizzbee.spark.kafka; import java.io.IOException; import java.time.Duration; import java.util.Arrays; import java.util.Collection; import java.util.Properties; import org.apache.kafka.clients.consumer.Consumer; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.consumer.ConsumerRecords; import org.apache.kafka.clients.consumer.KafkaConsumer; public class SimpleKafkaConsumer { public static void main(String[] args) { Properties props = new Properties(); //集群地址,多个地址用","分隔 //因为生产者像三个broker生产,所以这边写93,94,95都行 props.put("bootstrap.servers","spark:9095"); //设置消费者的group id props.put("group.id", "group1"); //如果为真,consumer所消费消息的offset将会自动的同步到zookeeper。如果消费者死掉时,由新的consumer使用继续接替 props.put("enable.auto.commit", "true"); //consumer向zookeeper提交offset的频率 props.put("auto.commit.interval.ms", "1000"); props.put("session.timeout.ms", "30000"); //反序列化 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<String, String>(props); // 订阅topic,可以为多个用,隔开,此处订阅了"test-partition-1", "test"这两个主题 consumer.subscribe(Arrays.asList("bizzbee_topic", "bizzbee-replicated-topic")); //持续监听 while(true){ //poll频率 ConsumerRecords<String,String> consumerRecords = consumer.poll(Duration.ofMillis(100)); for(ConsumerRecord<String,String> consumerRecord : consumerRecords){ System.out.println("bizzbee-replicated-topic" + consumerRecord.value()); } } } } ~~~ * 把上一节的生产者运行起来,然后运行消费者就可以了。 ![](https://img.kancloud.cn/17/ac/17ac9c99b05c7480ff0616a966f36cf1_503x281.png)