如何使用 Confluent.Kafka .Net Client 创建 Kafka Topic [英] How to create a Kafka Topic using Confluent.Kafka .Net Client
问题描述
它似乎是最流行的 Kafka .net 客户端(https://github.com/confluentinc/confluent-kafka-dotnet) 缺少设置和创建主题的方法.调用 Producer.ProduceAsync()
时,主题会自动创建,但我找不到设置分区、保留策略和其他设置的方法.
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.
也许我可以使用另一个 .net 客户端?
Maybe there is another .net client that I can use instead?
推荐答案
现在在最新版本的 Confluent.Kafka .Net 客户端库中可用.
It is now available in latest release of Confluent.Kafka .Net client library.
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}");
}
}
这篇关于如何使用 Confluent.Kafka .Net Client 创建 Kafka Topic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!