英文:
How to list all available brokers in Kafka cluster using Java API?
问题
仅翻译内容如下:
标题所述。
是否有任何方式(使用Kafka Java API)来获取集群中可用的Kafka代理数量?
我知道使用随Kafka服务器一起提供的命令行工具可以完成此操作;但是我还没有找到如何使用Java API来实现这一点。
英文:
What the title says.
Is there anyway (using Kafka Java API) to get the number of AVAILABLE Kafka brokers in a cluster?
I know that with the command line utility that comes with the Kafka server, this can be done; but I haven't found anything on how to do it using Java API.
答案1
得分: 2
你可以从kafkaAdminClient.describeCluster获取代理(broker)和集群信息。
> 使用默认选项获取集群中节点的信息。这是一个方便的方法,用于带有默认选项的#describeCluster(DescribeClusterOptions)。有关更多详细信息,请参阅重载方法。
Properties properties = new Properties();
properties.setProperty("bootstrap.servers", "localhost:9092");
properties.setProperty("client.id", "producerAdmin");
properties.setProperty("metadata.max.age.ms", "3000");
kafkaAdminClient = AdminClient.create(properties);
DescribeClusterResult result = kafkaAdminClient.describeCluster();
KafkaFuture<Collection<Node>> nodes = result.nodes();
您还可以在Node类中使用host和port方法来获取更多信息。
英文:
You can get the broker's and cluster information from kafkaAdminClient.describeCluster
>Get information about the nodes in the cluster, using the default options. This is a convenience method for #describeCluster(DescribeClusterOptions) with default options. See the overload for more details.
Properties properties = new Properties();
properties.setProperty("bootstrap.servers", "localhost:9092");
properties.setProperty("client.id", "producerAdmin");
properties.setProperty("metadata.max.age.ms", "3000");
kafkaAdminClient = AdminClient.create(properties);
DescribeClusterResult result = kafkaAdminClient.describeCluster()
KafkaFuture<Collection<Node>> nodes = result.nodes();
And you can use host,port methods in Node class to get more information
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论