How to create a Kafka Topic using Confluent.Kafka .Net Client

10,827

It is now available in latest release of Confluent.Kafka .Net client library.

See: https://github.com/confluentinc/confluent-kafka-dotnet/blob/b7b04fed82762c67c2841d7481eae59dee3e4e20/examples/AdminClient/Program.cs

        using (var adminClient = new AdminClientBuilder(new AdminClientConfig { BootstrapServers = bootstrapServers }).Build())
        {
            try
            {
                await adminClient.CreateTopicsAsync(new TopicSpecification[] { 
                    new TopicSpecification { Name = topicName, ReplicationFactor = 1, NumPartitions = 1 } });
            }
            catch (CreateTopicsException e)
            {
                Console.WriteLine($"An error occured creating topic {e.Results[0].Topic}: {e.Results[0].Error.Reason}");
            }
        }
Share:
10,827
Michael D.
Author by

Michael D.

.Net developer by daily job and Ruby On Rails hobbyist SOreadytohelp

Updated on June 12, 2022

Comments

  • Michael D.
    Michael D. almost 2 years

    It seems like most popular .net client for Kafka (https://github.com/confluentinc/confluent-kafka-dotnet) is missing methods to setup and create Topics. When calling Producer.ProduceAsync() the topic is created automatically but I can't find a way to setup partitions, retention policy and other settings.

    I tried to find any examples online but all I found just use defaults.

    Maybe there is another .net client that I can use instead?

  • Michael D.
    Michael D. almost 6 years
    The problem that I am facing is still exists: the topic will be created with all default settings. Simple call to Producer.ProduceAsync() also creates a topic if not exists
  • OneCricketeer
    OneCricketeer over 2 years
    Answer needs to be updated with the correct api, or clarify this will use default topic settings