1、将kafka里lib目录下(除jar包外还有别的东西)所有的jar包导入工程中。

2、代码

public static void main(String[] args) {
//声明连接属性
Properties properties = new Properties();
properties.put("zookeeper.connect", "192.168.157.131:2181");//声明zk
properties.put("group.id", "g_1");// 必须要使用别的组名称, 如果生产者和消费者都在同一组,则不能访问同一组内的topic数据
properties.put("auto.offset.reset", "smallest");
//连接kafka
ConsumerConnector consumer = Consumer.createJavaConsumerConnector(new ConsumerConfig(properties));

//消费数据
Map<String, Integer> confMap = new HashMap();
confMap.put("test", 1);  //“test"为主题名,1为每次获取数据的条数
Map<String, List<KafkaStream<byte[], byte[]>>> ms = consumer.createMessageStreams(confMap);
KafkaStream<byte[], byte[]> ks = ms.get("test").get(0);  //”test"为要消费的主题
ConsumerIterator<byte[], byte[]> it = ks.iterator();
while(it.hasNext()){
MessageAndMetadata<byte[], byte[]> next = it.next();
byte[] message = next.message();
System.out.println(Arrays.toString(message));
}
//断开连接
consumer.shutdown();
}

相关文章:

  • 2021-09-21
  • 2021-11-20
  • 2021-11-20
  • 2021-06-21
  • 2021-12-27
  • 2021-12-12
  • 2021-11-18
猜你喜欢
  • 2022-12-23
  • 2021-07-26
  • 2021-09-06
  • 2022-12-23
  • 2022-12-23
  • 2022-01-06
  • 2022-12-23
相关资源
相似解决方案