In this blog post, we'll explore how to create a real-time CSV file generation system using Kafka consumers in a C# application. Our goal is to configure up to 5 Kafka consumers, read data from a database table, and generate a CSV file whenever an update operation occurs in the table. We'll walk through each step of the process and provide code snippets to illustrate the implementation.
1. Setting Up Kafka Consumers in appsettings.json
First, let's configure Kafka consumers in the appsettings.json file. We'll define settings for up to 5 consumers, specifying topics, group IDs, and other relevant parameters:
{
"KafkaConsumers": {
"Consumer1": {
"Topic": "topic1",
"GroupId": "group1"
},
"Consumer2": {
"Topic": "topic2",
"GroupId": "group2"
},
// Add settings for Consumer3, Consumer4, and Consumer5 as needed
}
}
2. Implementing Kafka Consumer in C#
Next, we'll implement Kafka consumers in our C# application using a Kafka client library such as Confluent.Kafka. We'll create a KafkaConsumer class responsible for initializing and consuming messages from Kafka topics:
using Confluent.Kafka;
using Microsoft.Extensions.Configuration;
using System;
using System.Threading;
public class KafkaConsumer
{
private readonly IConfiguration _configuration;
public KafkaConsumer(IConfiguration configuration)
{
_configuration = configuration;
}
public void ConsumeMessages(string consumerName)
{
var consumerConfig = _configuration.GetSection($"KafkaConsumers:{consumerName}").Get<ConsumerConfig>();
using (var consumer = new ConsumerBuilder<Ignore, string>(consumerConfig).Build())
{
consumer.Subscribe(consumerConfig.Topic);
CancellationTokenSource cts = new CancellationTokenSource();
Console.CancelKeyPress += (_, e) =>
{
e.Cancel = true; // prevent the process from terminating.
cts.Cancel();
};
try
{
while (true)
{
try
{
var message = consumer.Consume(cts.Token);
Console.WriteLine($"Received message: {message.Value} from topic {message.Topic} at partition {message.Partition}, offset {message.Offset}");
// Process the received message and generate CSV file
GenerateCSV(message.Value);
}
catch (ConsumeException e)
{
Console.WriteLine($"Error occurred: {e.Error.Reason}");
}
}
}
catch (OperationCanceledException)
{
// Ensure the consumer leaves the group cleanly and final offsets are committed.
consumer.Close();
}
}
}
private void GenerateCSV(string message)
{
// Logic to parse the message, fetch data from the database, and generate CSV file
// This will vary based on the structure of your message and database schema
}
}
3. Database Connectivity and CSV Generation
To generate the CSV file based on data from the database table, we'll need to establish a database connection and implement logic to retrieve data and convert it into CSV format. Here's a simplified example using Entity Framework Core:
public class DatabaseContext : DbContext
{
public DbSet<YourEntity> YourEntities { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("your_connection_string");
}
}
public class YourEntity
{
// Define properties corresponding to your database table columns
}
4. Integrating Kafka Consumers into the Main Program
Finally, let's integrate the Kafka consumers into our main program:
class Program
{
static void Main(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var consumerNames = configuration.GetSection("KafkaConsumers").GetChildren().Select(c => c.Key);
foreach (var consumerName in consumerNames)
{
var kafkaConsumer = new KafkaConsumer(configuration);
new Thread(() => kafkaConsumer.ConsumeMessages(consumerName)).Start();
}
Console.ReadLine(); // Keep the program running
}
}
Below is an end-to-end C# code example for the described approach. Please note that this example assumes the usage of Entity Framework Core for database operations, Confluent.Kafka for Kafka integration and AWS SDK for .NET for S3 interaction. You'll need to install the necessary NuGet packages for these dependencies.

Join the conversation! Your thoughts help the community grow.