You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何查看Apache Kafka Broker中的分区及分区与Broker的对应关系?

查看Apache Kafka Broker与分区的对应关系

一、用kafkacat查看分区与Broker的映射

你使用的.\bin\win\kafkacat -L -b hostname:port命令其实已经包含了分区与Broker的对应信息,只是需要聚焦输出里的分区详情模块。典型输出示例:

Metadata for all topics (from broker 0: hostname:port/0):
 1 brokers:
  broker 0 at hostname:port (controller)
 2 topics:
  topic "test-topic" with 3 partitions:
    partition 0, leader 1, replicas: 1,0,2, isrs: 1,0,2
    partition 1, leader 0, replicas: 0,2,1, isrs: 0,2,1
    partition 2, leader 0, replicas: 0,1,2, isrs: 0,1,2

其中:

  • leader X表示该分区的主Broker ID
  • replicas: x,y,z表示该分区所有副本分布的Broker ID列表

如果输出太冗长,可以指定具体topic来简化输出:

.\bin\win\kafkacat -L -b hostname:port -t your-target-topic

二、Kafka自带工具查看

Windows环境下可以用官方提供的kafka-topics.bat命令,输出更结构化:

.\bin\windows\kafka-topics.bat --describe --bootstrap-server hostname:port --topic your-target-topic

输出里的Leader列对应分区主Broker,Replicas列是所有副本所在Broker,Isr列是当前同步的存活副本Broker。

三、快速整理Broker与分区的对应关系

如果需要手动整理成「Broker X对应分区Y、Z」的格式,可以用PowerShell脚本批量处理kafkacat的输出:

# 替换成你的Broker地址和目标topic
$broker = "hostname:port"
$topic = "your-target-topic"

.\bin\win\kafkacat -L -b $broker -t $topic | Select-String "partition (\d+), leader (\d+)" | ForEach-Object {
    $lineParts = $_.Line -split ", "
    $partition = ($lineParts[0] -split " ")[1]
    $leaderBroker = ($lineParts[1] -split " ")[1]
    [PSCustomObject]@{BrokerID = $leaderBroker; PartitionID = $partition}
} | Group-Object BrokerID | ForEach-Object {
    Write-Host "Broker $($_.Name) 对应分区: $($_.Group.PartitionID -join ', ')"
}

这个脚本会自动统计每个Broker作为主节点对应的分区列表。

内容的提问来源于stack exchange,提问作者Bigsol

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 07:25:32