kafka消费者示范代码(Java)
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();
}