Kafka
C#
Coding
Programming Tutorials
Topic List Generation

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.

Practice system design

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:

bash
dotnet add package Confluent.Kafka

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.

csharp
var config = new AdminClientConfig { BootstrapServers = "localhost:9092" };

3. Create an Admin Client

Using the AdminClientBuilder, build an admin client. This client will be used to retrieve metadata from the Kafka cluster.

csharp
using var adminClient = new AdminClientBuilder(config).Build();

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.

csharp
var metaData = adminClient.GetMetadata(TimeSpan.FromSeconds(5)); // Timeout of 5 seconds

5. Display or Process Topics

Extract the topic list from the metadata and process it per your application's needs.

csharp
1foreach (var topic in metaData.Topics)
2{
3    Console.WriteLine($"Topic: {topic.Topic}");
4}

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

StepActionCode SnippetDescription
1Install Confluent.Kafkadotnet add package Confluent.KafkaAdds Kafka client library to project.
2Configure Kafka Clientvar config = new AdminClientConfig { BootstrapServers = "localhost:9092" };Setup configuration with server details.
3Create Admin Clientusing var adminClient = new AdminClientBuilder(config).Build();Initialize an admin client.
4Retrieve Topicsvar metaData = adminClient.GetMetadata(TimeSpan.FromSeconds(5));Fetch list of topics from server.
5Display Topicsforeach (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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.