Introduction
In this tutorial, we’ll explore how to develop a C# client application that produces and consumes messages from an Apache Kafka® cluster. Kafka is a powerful distributed streaming platform that allows you to build real-time data pipelines and event-driven applications.
Prerequisites
Before we begin, make sure you have the following prerequisites:
- .NET SDK: Ensure you have the .NET SDK installed on your machine.
- Confluent Cloud Account (Optional): While setting up a local Kafka cluster is an option, I recommend using Confluent Cloud for a hassle-free experience. You can sign up for a free account without needing a credit card1.
Getting Started
1. Create a New Project
Start by creating a new C# project. You can use Visual Studio, Visual Studio Code, or any other preferred IDE.
2. Install the Confluent Kafka NuGet Package
To interact with Kafka, install the Confluent.Kafka NuGet package. You can do this via the NuGet Package Manager or the command line:
dotnet add package Confluent.Kafka
3. Configure Kafka Connection
If you’re using Confluent Cloud, retrieve your Kafka cluster credentials (bootstrap servers, API keys, etc.). Otherwise, set up a local Kafka cluster and note down the connection details.
4. Build a Producer
Create a Kafka producer that sends messages to a specific topic. Here’s a basic example:
using Confluent.Kafka;
var config = new ProducerConfig
{
BootstrapServers = "your-bootstrap-servers",
SaslMechanism = SaslMechanism.Plain,
SecurityProtocol = SecurityProtocol.SaslSsl,
SaslUsername = "your-api-key",
SaslPassword = "your-api-secret"
};
using var producer = new ProducerBuilder<Null, string>(config).Build();
var topic = "my-topic";
var message = new Message<Null, string> { Value = "Hello, Kafka!" };
producer.Produce(topic, message);
5. Build a Consumer
Create a Kafka consumer that subscribes to the same topic and processes incoming messages. Here’s a simple example:
using Confluent.Kafka;
var config = new ConsumerConfig
{
BootstrapServers = "your-bootstrap-servers",
GroupId = "my-consumer-group",
AutoOffsetReset = AutoOffsetReset.Earliest
};
using var consumer = new ConsumerBuilder<Ignore, string>(config).Build();
consumer.Subscribe("my-topic");
while (true)
{
var result = consumer.Consume();
Console.WriteLine($"Received: {result.Message.Value}");
}
Conclusion
Congratulations! You’ve built a basic Kafka application in C#. Feel free to explore more advanced features like message partitioning, error handling, and serialization.
Remember to adapt the code to your specific Kafka setup and requirements. Happy Kafka-ing! 🚀
References:
- Getting Started with Apache Kafka and .NET1
- Apache Kafka C#.NET – Producer and Consumer Examples2
- How To Use Apache Kafka In .NET Application3