How to get topics list from Kafka using C#
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a distributed event streaming platform capable of handling trillions of events a day. Integrating Kafka with various programming languages is crucial for creating flexible, scalable applications. In C#, Kafka can be accessed using the Confluent Kafka client. This article details how to retrieve a list of topics from a Kafka cluster using C#.
Prerequisites
To begin, ensure you have the following:
- .NET environment set up (recommended: .NET Core 3.1 or later)
- Kafka server running and accessible
- Confluent.Kafka NuGet package installed in your project
Step-by-Step Guide
1. Install the Confluent.Kafka Package
First, add the Confluent.Kafka NuGet package to your C# project. You can add it via the NuGet Package Manager in Visual Studio or by running the following command in the terminal:
2. Configuring the Kafka Client
Create an instance of ConsumerConfig or AdminClientConfig with the minimum required properties set, such as BootstrapServers, which is the Kafka server URL.
3. Create an Admin Client
Using the AdminClientBuilder, build an admin client. This client will be used to retrieve metadata from the Kafka cluster.
4. Retrieve Topics List
You can retrieve the list of topics using the GetMetadata method, which returns metadata for all topics, or specify particular topics if needed.
5. Display or Process Topics
Extract the topic list from the metadata and process it per your application's needs.
This loop will print out the names of all topics on the Kafka server.
Potential Issues and Solutions
When retrieving the topics list, you may encounter issues such as network errors or configuration mismatches. Here are some tips to handle them:
- Ensure that the Kafka server is running and accessible from your network.
- Verify that all configurations, such as
BootstrapServers, are correctly set. - Implement appropriate error handling around Kafka operations to manage exceptions gracefully.
Summary Table
| Step | Action | Code Snippet | Description |
| 1 | Install Confluent.Kafka | dotnet add package Confluent.Kafka | Adds Kafka client library to project. |
| 2 | Configure Kafka Client | var config = new AdminClientConfig { BootstrapServers = "localhost:9092" }; | Setup configuration with server details. |
| 3 | Create Admin Client | using var adminClient = new AdminClientBuilder(config).Build(); | Initialize an admin client. |
| 4 | Retrieve Topics | var metaData = adminClient.GetMetadata(TimeSpan.FromSeconds(5)); | Fetch list of topics from server. |
| 5 | Display Topics | foreach (var topic in metaData.Topics) { Console.WriteLine($"Topic: {topic.Topic}"); } | Print topic names to console. |
Conclusion
Retrieving a list of topics from a Kafka server using C# and Confluent.Kafka is a straightforward process requiring initialization of an admin client and making a request for metadata. Proper configuration and error handling enhance robustness and reliability of the code. The ability to interact programmatically with Kafka provides a powerful tool for developers working on distributed systems and microservices.
Related reading
- How to get windowed aggregation from kafka stream?
- How to gracefully shutdown spring-kafka consumer application
- How to Guarantee Message delivery with Celery?
- How to guarantee order in Kafka partition
- How to git-pull a given patch set from Gerrit?
- How to group by the elements of an array in Swift
- how to get value from appsettings.json
- How to get Xml as string from XDocument?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.