• 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();
    }

  • 相关阅读:
    我所知道的数据库8-DML语言
    我所知道的数据库7-DDL语言(续2)
    CSS3 3D transform变换
    深入理解Node.js的异步编程风格(转载)
    ECMAscript
    JavaScript高级部分概念用法
    前端工程师面试题汇总
    10个最常见的HTML5中的面试题及答案
    js事件流
    本地储存
  • 原文地址:https://www.cnblogs.com/runnerjack/p/8613696.html
Copyright © 2020-2023  润新知