![](/img/trans.png)
[英]How to create a Kafka Topic using Confluent.Kafka .Net Client
[英]Is there a way to query the replication factor and retention time for a topic using the Confluent.Kafka .Net client?
Confluent.Kafka AdminClient 允许您创建一个主题,指定名称、分区数、复制因子和保留(我猜测通过 configs 属性进行其他设置)。 然而,GetMetadata() 调用返回一个 TopicMetadata,其中只有名称和分区信息。 有没有办法使用 .Net 客户端检索复制因子和保留时间?
await adminClient.CreateTopicsAsync(new[]
{
new TopicSpecification
{
Name = topicName,
NumPartitions = _connectionSettings.TopicAutoCreatePartitionCount,
ReplicationFactor = _connectionSettings.TopicAutoCreatePartitionCount,
Configs = new Dictionary<string, string> {{"retention.ms", "9999999999999"}}
}
});
要获取保留时间,您可以使用DescribeConfigsAsync
:
var results = await adminClient.DescribeConfigsAsync(new[] { new ConfigResource { Name = "topic_name", Type = ResourceType.Topic } });
foreach (var result in results)
{
var retentionConfig = result.Entries.SingleOrDefault(e => e.Key == "retention.ms");
}
但我不确定获取复制因子的正确方法是什么,因为它不是使用DescribedConfigsAsync
检索的。 我能想到的一种方法是使用GetMetadata
但这不是一个非常干净的解决方案:
var meta = adminClient.GetMetadata(TimeSpan.FromSeconds(5));
var topic = meta.Topics.SingleOrDefault(t => t.Topic == "topic_name");
var replicationFactor = topic.Partitions.First().Replicas.Length;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.