How to get topics list from Kafka using C#

10.3k Views Asked by At

I want to get topics list from Kafka. I'm using kafka-net client but unable to find in documentation about fetching topics list.

2

There are 2 best solutions below

0
On

You can list all topics using the AdminClient available in Confluent.Kafka package:

using Confluent.Kafka;
using Confluent.Kafka.Admin;

var adminConfig = new AdminClientConfig()
{
   BootstrapServers = "SERVER_URL"
};

using (var adminClient = new AdminClientBuilder(adminConfig).Build())
{
   var metadata = adminClient.GetMetadata(TimeSpan.FromSeconds(10));
   var topicsMetadata = metadata.Topics;
   var topicNames = metadata.Topics.Select(a => a.Topic).ToList();
}
1
On

According to the documentation you should be able to do this with

producer.GetMetadata(true, null)

public Metadata GetMetadata(bool allTopics, string topic)

Query the cluster for metadata (blocking).

allTopics = true - request all topics from cluster

https://docs.confluent.io/4.0.1/clients/confluent-kafka-dotnet/api/Confluent.Kafka.Producer.html#Confluent_Kafka_Producer_GetMetadata_System_Boolean_System_String_