HomeLinuxApache Kafka Shopper Instance in C#

Apache Kafka Shopper Instance in C#


C#, pronounced as C-Sharp, is without doubt one of the hottest programming languages of the fashionable age. It’s a strongly-types, objected-oriented programming language which contains helpful programming options equivalent to crucial, declarative, purposeful, and generic programming paradigms.

Apart from the intensive use in desktop purposes and video video games, C# permits the builders to put in writing intensive and strong net purposes utilizing the blazor and extra.

This tutorial teaches us how one can create a Kafka client consumer software utilizing the C# programming language.

What Is a Kafka Shopper?

In case you are beginning with Apache Kafka, try our collection to find extra. Nonetheless, a Kafka client is a straightforward software that the subscribes to a selected Kafka subject inside a Kafka cluster receives messages from a given partition. A Kafka client makes use of a pull-based mannequin to eat the obtainable messages on the outlined tempo and supplies dependable message processing utilizing configurable offset values.

Necessities:

To observe together with this tutorial and produce a working C# client app, you want the next:

  1. .NET Core SDK 2.1 or increased
  2. Confluent.Kafka NuGet package deal
  3. Apache Kafka cluster

Allow us to dive in!

Setup Pattern Matter

Step one is to create the subject that we’ll subscribe to utilizing the buyer software. We are able to use the Kafka CLI instruments for that. The command is as proven within the following:

./bin/kafka-topics –create
  –topic customers
  –bootstrap-server localhost:9092
  –replication-factor 1
  –partitions 1

The given command ought to create a brand new subject referred to as “customers” with the required properties.

Venture Setup

Allow us to now setup our undertaking. First, it’s good to keep in mind that this tutorial assumes you could have the most recent model of the .NET SDK and Visible Studio IDE.

Open the Visible Studio IDE and choose “Create a New Venture” from the “Get Began” web page.

Seek for “Console App” within the template part.

The following step is configuring the undertaking particulars such because the undertaking title, listing location, and many others.

Select “.NET 6.0” within the “Framework” choice.

As soon as accomplished, open the “program.cs” file and add the supply code for the buyer and producer purposes. The supply code is offered as follows:

utilizing Confluent.Kafka;

utilizing System;

class Program

{

  static void Foremost(string[] args)

  {

    var config = new ProducerConfig { BootstrapServers = “172.17.148.4:9092” };

    utilizing var producer = new ProducerBuilder<Null, string>(config).Construct();

    for (int i = 0; i < 10; ++i)

    {

      producer.Produce(“customers”, new Message<Null, string> { Worth = $“Whats up, Kafka consumer! ({i})” });

    }

    producer.Flush(TimeSpan.FromSeconds(10));

    Console.WriteLine(“Produced 10 messages to subject customers”);

    var consumerConfig = new ConsumerConfig

    {

      BootstrapServers = “172.17.148.4:9092”,

      GroupId=“group1”,

      AutoOffsetReset = AutoOffsetReset.Earliest

    };

    utilizing var client = new ConsumerBuilder<Ignore, string>(consumerConfig).Construct();

    client.Subscribe(“customers”);

    attempt

    {

        whereas (true)

        {

        var consequence = client.Eat(TimeSpan.FromSeconds(1));

        if (consequence == null)

        {

        proceed;

    }

    Console.WriteLine($“Consumed message ‘{consequence.Message.Worth}’ at: ‘{consequence.TopicPartitionOffset}’.”);

     }

     }

      catch (Exception ex)

      {

          Console.WriteLine($“Error: {ex.Message}”);

      }

  }

}

Word that the earlier code requires you to put in the Confluent.Kafka package deal. You are able to do this by heading to the Instruments -> Nuget Bundle Supervisor – Packet Supervisor Console and working the next command:

Set up-Bundle Confluent.Kafka -Model 2.0.2

This command downloads the required package deal and installs it in your undertaking.

Allow us to break down what the undertaking entails:

Within the earlier program, we began by importing the required packages. On this case, we’d like the System and Confluent.kafka packages.

Subsequent, we create a ProducerConfig object with the handle of the Kafka bootstrap server. You may change this worth to mirror the handle of your Kafka cluster.

We then use the ProducerBuilder to create a brand new Kafka producer occasion with NULL keys and string values. The producer then writes ten messages to the cluster customers’ subject.

Within the subsequent part, we use the ConsumerConfig and cross the handle to the Kafka server and the goal client group ID. We additionally outline the AutoOffsetReset parameter and set its worth to “Earliest”.

We then use this configuration to create a ConsumerBuilder to construct a Kafka client of “Ignore” keys and string values. Lastly, we use the buyer to subscribe to the customers’ subject and enter a loop to repeatedly ballot for the messages utilizing the “Eat” methodology with a timeout of 1 second.

As soon as the message is acquired, the buyer prints the message worth and subject partition offset to the console.

You may construct and run the undertaking from the Visible Studio IDE.

Conclusion

You now realized how we will arrange a Kafka producer and client software utilizing the .NET framework by the Confluent.Kafka package deal.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments