Java 获取 Kafka 的 topic 列表

命令行获取

根据 zookeeper 地址获取 topic:

$ bin/kafka-topics.sh --list --zookeeper 127.0.0.1:2181

引入依赖获取

根据 kafka 地址

引入依赖

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka-clients</artifactId>
    <version>${kafka.version}</version>
</dependency>

根据 kafka 地址获取 topic 的 Java 方法

Properties pro = new Properties();
pro.put("bootstrap.servers", KAFKA_BOOTSTRAP_SERVERS);
ListTopicsResult result = KafkaAdminClient.create(pro).listTopics();
KafkaFuture<Set<String>> set = result.names();
Set<String> topicSet = set.get();

根据 zookeeper 地址

引入依赖

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka_2.12</artifactId>
    <version>${kafka.version}</version>
</dependency>

根据 zookeeper 地址获取 topic 的 Java 方法

ZkUtils zkUtils = ZkUtils.apply(KAFKA_ZOOKEEPER_SERVERS, 30000, 30000, JaasUtils.isZkSecurityEnabled());
Seq<String> topicSeq = zkUtils.getAllTopics();
List<String> allTopicList = JavaConversions.seqAsJavaList(topicSeq);