简体   繁体   中英

How to create new topic in c# by using kafka-net

I have HomeController as below,

#region Properties

const string topic = "AnotherTestTopic";
const string host = "http://localhost:9092";

#endregion


[HttpPost]
public ActionResult Save(FormCollection form)
{
    var kafkaOptions = new KafkaOptions(new Uri(host));

    var brokerRouter = new BrokerRouter(kafkaOptions);

    var producer = new Producer(brokerRouter);

    producer.SendMessageAsync(topic, new[] { new Message("Test message") }).Wait();

    return RedirectToAction("Index", "Home");
}

I am using kafka-net dll and my SendMessageAsync method as below

   public async Task<List<ProduceResponse>> SendMessageAsync(string topic, IEnumerable<Message> messages, Int16 acks = 1,
            TimeSpan? timeout = null, MessageCodec codec = MessageCodec.CodecNone)
        {
            if (_stopToken.IsCancellationRequested)
                throw new ObjectDisposedException("Cannot send new documents as producer is disposing.");
            if (timeout == null) timeout = TimeSpan.FromMilliseconds(DefaultAckTimeoutMS);

            var batch = messages.Select(message => new TopicMessage
            {
                Acks = acks,
                Codec = codec,
                Timeout = timeout.Value,
                Topic = topic,
                Message = message
            }).ToList();

            _asyncCollection.AddRange(batch);

            await Task.WhenAll(batch.Select(x => x.Tcs.Task));

            return batch.Select(topicMessage => topicMessage.Tcs.Task.Result)
                                .Distinct()
                                .ToList();
        }

Question:

I just started learning kafka.I really do not know how can i create topic from c# code.How can i add topic from c# ?

Any help will be appreciated.

Thanks.

You can set auto.create.topics.enable=true in your broker configuration to let Kafka create the topic for you when it is not previously created.

You may also want to set num.partitions and default.replication.factor to appropriate values in your broker configuration as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM