• java api如何获取kafka所有Topic列表,并放置为一个list


    kafka内部所有的实现都是通过TopicCommand的main方法,通过java代码调用API,TopicCommand.main(options)的方式只能打印到控制台,不能转换到一个list。

    下面讲解下如何转换为list:

    1、查看主题(Topic)

    【命令方式】:bin/kafka-topics.sh --list --zookeeper 192.168.2.212:2181/kafka

    【JAVA API方式】:

    public static void main(String[] args) {
    String[] options = new String[]{ 
    "--list", 
    "--zookeeper", 
    "192.168.2.212:2181/kafka"
    
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024*3);
    // cache stream
    PrintStream cacheStream = new PrintStream(byteStream);
    // old stream
    PrintStream oldStream = System.out;
    
    
    
    System.setOut(cacheStream);
    
    TopicCommand.main(options);
    
    String message = byteStream.toString();
    List<String> ls = new ArrayList<String>();
    
    String[]ss = message.split("
    ");
    ls = Arrays.asList(ss);
    
    // Restore old stream
    System.setOut(oldStream);
    
    for(int i=0;i<ss.length;i++){//循环遍历转换后的list中的topic
      System.out.println(ls.get(i));
    }
    
    }
  • 相关阅读:
    第十五周作业
    第十四周作业
    第十三周
    第十二周作业
    第二次考试
    太强了,大佬开源的算法小抄彻底火了
    npm--npm 全局安装路径的修改和环境变量的配置
    239
    238
    学术类论文查看--AMiner
  • 原文地址:https://www.cnblogs.com/tanglc/p/5251545.html
Copyright © 2020-2023  润新知