简体   繁体   English

如何从Java中获取kafka服务器的主题列表

[英]How to get topic list from kafka server in Java

I am using kafka 0.8 version and very much new to it. 我正在使用kafka 0.8版本,非常新的。

I want to know the list of topics created in kafka server along with it's metadata. 我想知道在kafka server创建的主题列表及其元数据。 Is there any API available to find out this? 是否有任何API可以找到这个?

Basically, I need to write a Java consumer that should auto-discover any topic in kafka server .There is API to fetch TopicMetadata , but this needs name of topic as input parameters.I need information for all topics present in server. 基本上,我需要编写一个Java消费者,它应该自动发现kafka server任何主题。有API来获取TopicMetadata ,但这需要topic的名称作为输入参数。我需要服务器中存在的所有主题的信息。

with Kafka 0.9.0 与Kafka 0.9.0

you can list the topics in the server with the provided consumer method listTopics(); 您可以使用提供的使用者方法listTopics()列出服务器中的主题;

eg. 例如。

Map<String, List<PartitionInfo> > topics;

Properties props = new Properties();
props.put("bootstrap.servers", "1.2.3.4:9092");
props.put("group.id", "test-consumer-group");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);
topics = consumer.listTopics();
consumer.close();

I think this is the best way: 我认为这是最好的方法:

ZkClient zkClient = new ZkClient("zkHost:zkPort");
List<String> topics = JavaConversions.asJavaList(ZkUtils.getAllTopics(zkClient));

A good place to start would be the sample shell scripts shipped with Kafka. 一个好的起点是Kafka附带的示例shell脚本。 In the /bin directory of the distribution there's some shell scripts you can use, one of which is ./kafka-topic-list.sh If you run that without specifying a topic, it will return all topics with their metadata. 在发行版的/ bin目录中,您可以使用一些shell脚本,其中一个是./kafka-topic-list.sh如果您在未指定主题的情况下运行该脚本,它将返回包含其元数据的所有主题。 See: https://github.com/apache/kafka/blob/0.8/bin/kafka-list-topic.sh 请参阅: https//github.com/apache/kafka/blob/0.8/bin/kafka-list-topic.sh

That shell script in turn runs: https://github.com/apache/kafka/blob/0.8/core/src/main/scala/kafka/admin/ListTopicCommand.scala 该shell脚本依次运行: https//github.com/apache/kafka/blob/0.8/core/src/main/scala/kafka/admin/ListTopicCommand.scala

The above are both references to the 0.8 Kafka version, so if you're using a different version (even a point difference), be sure to use the appropriate branch/tag on github 以上都是对0.8 Kafka版本的引用,所以如果你使用的是不同的版本(甚至是点差),请务必在github上使用相应的分支/标签

If you want to pull broker or other-kafka information from Zookeeper then kafka.utils.ZkUtils provides a nice interface. 如果你想从Zookeeper中提取代理或其他kafka信息,那么kafka.utils.ZkUtils提供了一个很好的界面。 Here is the code I have to list all zookeeper brokers (there are a ton of other methods there): 这是我要列出所有zookeeper经纪人的代码(那里有很多其他方法):

List<Broker> listBrokers() {

        final ZkConnection zkConnection = new ZkConnection(connectionString);
        final int sessionTimeoutMs = 10 * 1000;
        final int connectionTimeoutMs = 20 * 1000;
        final ZkClient zkClient = new ZkClient(connectionString,
                                               sessionTimeoutMs,
                                               connectionTimeoutMs,
                                               ZKStringSerializer$.MODULE$);

        final ZkUtils zkUtils = new ZkUtils(zkClient, zkConnection, false);

        scala.collection.JavaConversions.seqAsJavaList(zkUtils.getAllBrokersInCluster());
}

Using Scala: 使用Scala:

import java.util.{Properties}
import org.apache.kafka.clients.consumer.KafkaConsumer

object KafkaTest {
  def main(args: Array[String]): Unit = {

    val brokers = args(0)
    val props = new Properties();
    props.put("bootstrap.servers", brokers);
    props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
    props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

    val consumer = new KafkaConsumer[String, String](props);
    val topics = consumer.listTopics().keySet();

    println(topics)
  }
}

You can use zookeeper API to get the list of brokers as mentioned below: 您可以使用zookeeper API获取下面提到的代理列表:

    ZooKeeper zk = new ZooKeeper("zookeeperhost, 10000, null);
    List<String> ids = zk.getChildren("/brokers/ids", false);
    List<Map> brokerList = new ArrayList<>();
    ObjectMapper objectMapper = new ObjectMapper();

    for (String id : ids) {
        Map map = objectMapper.readValue(zk.getData("/brokers/ids/" + id, false, null), Map.class);
        brokerList.add(map);
    }

Use this broker list to get all the topic using the following link 使用此代理列表可使用以下链接获取所有主题

https://cwiki.apache.org/confluence/display/KAFKA/Finding+Topic+and+Partition+Leader https://cwiki.apache.org/confluence/display/KAFKA/Finding+Topic+and+Partition+Leader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM