简体   繁体   English

如何获取 Confluent.Kafka 中某个主题的所有“PartitionTopic”列表?

[英]How can I get a list of all `PartitionTopic`s for a topic in Confluent.Kafka?

I am using confluent-kafka-dotnet (Confluent.Kafka) to produce messages for Kafka.我正在使用confluent-kafka-dotnet (Confluent.Kafka) 为 Kafka 生成消息。

I would like to manually manage which partitions messages go to in order to preserve order for certain groups of messages.我想手动管理哪些分区消息 go 以保留某些消息组的顺序。

How is it possible to get a list of PartitionTopic for a Kafka topic?如何获取 Kafka 主题的PartitionTopic列表?

I have looked at this solution in Java.我在 Java 中查看了这个解决方案 But I don't understand how to achieve the same functionality with Confluent.Kafka .但我不明白如何使用Confluent.Kafka实现相同的功能。

Alternatively, it would be acceptable to send messages with keys, because the same keys are guaranteed to be on the same partitions.或者,使用密钥发送消息是可以接受的,因为可以保证相同的密钥位于相同的分区上。 But again, I could not find a way to create a new Message with any key type other than Null .但同样,我找不到使用new Message以外的任何密钥类型创建Null的方法。 So, an example of sending a message with a non-null key would be helpful.因此,使用非空键发送消息的示例会有所帮助。

To get a list of TopicPartitions for a single topic you could use AdminClient class:要获取单个主题的 TopicPartitions 列表,您可以使用 AdminClient class:

using (var adminClient = new AdminClientBuilder(new AdminClientConfig { BootstrapServers = "bootstrap-servers" }).Build())
{
    var meta = adminClient.GetMetadata(TimeSpan.FromSeconds(20));

    var topic = meta.Topics.SingleOrDefault(t => t.Topic == "topic-name");

    var topicPartitions = topic.Partitions;
}

You can find more AdminClient examples here .您可以在此处找到更多 AdminClient 示例。 But notice the warnings, saying that: "The API for this functionality is subject to change."但请注意警告,上面写着:“此功能的 API 可能会发生变化。”

I have worked out that the reason I could not create messages with a non-null key was because I had specified my Producer with <Null, ...> .我已经弄清楚我无法使用非空键创建消息的原因是因为我使用<Null, ...>指定了我的 Producer。

Thank you to mjwills for asking me for minimal reproducible example, which prompted me to work this out.感谢mjwills要求我提供最小的可重现示例,这促使我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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