是否可以使用Confluent.Kafka .Net客户端查询主题的复制因子和保留时间?

问题描述

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;

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...