首页 > 解决方案 > 解释 GetMetadata 参数

问题描述

作为函数的文档GetMetaData

// GetMetadata queries broker for cluster and topic metadata.
// If topic is non-nil only information about that topic is returned, else if
// allTopics is false only information about locally used topics is returned,
// else information about all topics is returned.
// GetMetadata is equivalent to listTopics, describeTopics and describeCluster in the Java API.
func (a *AdminClient) GetMetadata(topic *string, allTopics bool, timeoutMs int) (*Metadata, error) {
    return getMetadata(a, topic, allTopics, timeoutMs)
}

所以我猜如果主题不是零,结果总是只返回特定的主题信息。但是,当我尝试测试时,我看到了非常奇怪的结果。

假设我已经有一个 Kafka 主题。然后我将该方法GetMetadata称为以下代码:

topic := "sample_topic"
admin.GetMetadata(nil, false, timeout)
admin.GetMetadata(nil, true, timeout)
admin.GetMetadata(&topic, false, timeout)
admin.GetMetadata(&topic, true, timeout)

admin.GetMetadata(&topic, false, timeout)返回主题信息。所有其他情况都返回所有主题信息。

但是,当测试集群中不存在的主题时,只有调用admin.GetMetadata(&topic, false, timeout)会产生错误。

data, err := admin.GetMetadata(&topic, false, admin.timeOutMs())
fmt.Println(data.Topics[topic]) //   Broker: Unknown topic or partition

重新排序代码时,结果也会发生变化。

我阅读了源代码,发现它使用了一些函数,librdkafka例如rd_kafka_metadataor _getMetadata_broker_element。但是我很难理解这些功能。

我用 3 个代理在 Kafka 集群上进行了测试。请为我解释背后的逻辑是什么。

标签: goapache-kafkaconfluent-platformlibrdkafka

解决方案


推荐阅读