📦 总览
Kafka 的 API 分层清晰,既适合构建简单的消息队列,也能支撑复杂的实时计算。下图展示了它们与 Kafka 集群的交互关系:
#mermaid-svg-wqknblzUx7AAQpYe{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-wqknblzUx7AAQpYe .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-wqknblzUx7AAQpYe .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-wqknblzUx7AAQpYe .error-icon{fill:#552222;}#mermaid-svg-wqknblzUx7AAQpYe .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-wqknblzUx7AAQpYe .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-wqknblzUx7AAQpYe .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-wqknblzUx7AAQpYe .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-wqknblzUx7AAQpYe .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-wqknblzUx7AAQpYe .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-wqknblzUx7AAQpYe .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-wqknblzUx7AAQpYe .marker{fill:#333333;stroke:#333333;}#mermaid-svg-wqknblzUx7AAQpYe .marker.cross{stroke:#333333;}#mermaid-svg-wqknblzUx7AAQpYe svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-wqknblzUx7AAQpYe p{margin:0;}#mermaid-svg-wqknblzUx7AAQpYe .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-wqknblzUx7AAQpYe .cluster-label text{fill:#333;}#mermaid-svg-wqknblzUx7AAQpYe .cluster-label span{color:#333;}#mermaid-svg-wqknblzUx7AAQpYe .cluster-label span p{background-color:transparent;}#mermaid-svg-wqknblzUx7AAQpYe .label text,#mermaid-svg-wqknblzUx7AAQpYe span{fill:#333;color:#333;}#mermaid-svg-wqknblzUx7AAQpYe .node rect,#mermaid-svg-wqknblzUx7AAQpYe .node circle,#mermaid-svg-wqknblzUx7AAQpYe .node ellipse,#mermaid-svg-wqknblzUx7AAQpYe .node polygon,#mermaid-svg-wqknblzUx7AAQpYe .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-wqknblzUx7AAQpYe .rough-node .label text,#mermaid-svg-wqknblzUx7AAQpYe .node .label text,#mermaid-svg-wqknblzUx7AAQpYe .image-shape .label,#mermaid-svg-wqknblzUx7AAQpYe .icon-shape .label{text-anchor:middle;}#mermaid-svg-wqknblzUx7AAQpYe .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-wqknblzUx7AAQpYe .rough-node .label,#mermaid-svg-wqknblzUx7AAQpYe .node .label,#mermaid-svg-wqknblzUx7AAQpYe .image-shape .label,#mermaid-svg-wqknblzUx7AAQpYe .icon-shape .label{text-align:center;}#mermaid-svg-wqknblzUx7AAQpYe .node.clickable{cursor:pointer;}#mermaid-svg-wqknblzUx7AAQpYe .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-wqknblzUx7AAQpYe .arrowheadPath{fill:#333333;}#mermaid-svg-wqknblzUx7AAQpYe .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-wqknblzUx7AAQpYe .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-wqknblzUx7AAQpYe .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wqknblzUx7AAQpYe .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-wqknblzUx7AAQpYe .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wqknblzUx7AAQpYe .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-wqknblzUx7AAQpYe .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-wqknblzUx7AAQpYe .cluster text{fill:#333;}#mermaid-svg-wqknblzUx7AAQpYe .cluster span{color:#333;}#mermaid-svg-wqknblzUx7AAQpYe div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-wqknblzUx7AAQpYe .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-wqknblzUx7AAQpYe rect.text{fill:none;stroke-width:0;}#mermaid-svg-wqknblzUx7AAQpYe .icon-shape,#mermaid-svg-wqknblzUx7AAQpYe .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wqknblzUx7AAQpYe .icon-shape p,#mermaid-svg-wqknblzUx7AAQpYe .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-wqknblzUx7AAQpYe .icon-shape .label rect,#mermaid-svg-wqknblzUx7AAQpYe .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wqknblzUx7AAQpYe .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-wqknblzUx7AAQpYe .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-wqknblzUx7AAQpYe :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Kafka 集群
Clients
写入
读取
读取
读取/写入
Source 拉取
Sink 推送
管理/监控
管理/监控
管理/监控
Producer API
数据生产
Consumer API
数据消费
Share Consumer API
协同消费
Streams API
流式处理
Connect API
数据管道
Admin API
集群管理
Broker 1
Broker 2
Broker 3
Topic A
Topic B
1️⃣ Producer API ------ 数据生产者
用途 :应用程序通过 Producer API 向 Kafka 集群的 Topic(主题) 发送数据流。
典型场景:日志收集、用户行为埋点、业务事件上报、监控指标推送等。
🔧 核心原理
- Producer 将消息封装为
ProducerRecord,指定目标 Topic 和可选的 Key、Value、分区(Partition)。 - 客户端内部通过 分区器(Partitioner) 决定消息发往哪个分区,默认按 Key 哈希或轮询。
- 消息以 批次(Batch) 形式发送,配合 缓冲池 和 异步发送 机制,显著提升吞吐量。
- 支持 确认机制(acks) :
acks=0(不等待)、acks=1(Leader 确认)、acks=-1/all(ISR 全部确认),以平衡性能和可靠性。
📦 Maven 依赖
xml
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>4.3.1</version>
</dependency>
2️⃣ Consumer API ------ 数据消费者
用途 :应用程序通过 Consumer API 从 Kafka Topic 中读取数据流。
典型场景:实时数据分析、监控告警、将数据写入数据库或缓存等。
🔧 核心原理
- Consumer 通过 订阅(subscribe) 或 手动指定分区(assign) 来消费消息。
- 消费进度由 消费偏移量(Offset) 控制,可自动或手动提交,保证 至少一次(at-least-once) 或 精确一次(exactly-once) 语义。
- 多个 Consumer 实例可组成 消费者组(Consumer Group) ,共同消费一个 Topic,实现 负载均衡 和 容错。
- 支持 拉取(Pull) 模型,消费者主动轮询 Broker 拉取消息,消费速率可控。
📦 Maven 依赖
(同 Producer,均为 kafka-clients)
xml
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>4.3.1</version>
</dependency>
3️⃣ Share Consumer API ------ 共享消费者(协同消费)
用途 :让同一个 共享组(Share Group) 中的多个消费者实例 协同 消费 Topic 中的消息,每个消息只会被组内 一个 消费者处理,但允许消费者独立提交偏移量,避免重平衡(Rebalance)带来的开销。
典型场景 :大规模并行消费且需要 细粒度确认 的作业,例如ETL任务、批处理系统。
🔧 核心原理
- 与普通 Consumer Group 不同,Share Consumer 采用 共享队列 模型,Broker 维护消息的"已分配"状态,消费者拉取后需显式 确认(Acknowledge) 处理完成。
- 若消费者未确认,Broker 可将消息重新分配给其他消费者,实现 容错。
- 避免了传统 Consumer Group 的 分区再平衡(Rebalance) 问题,适合处理时间不均匀的长任务。
📌 这是 Kafka 4.3 引入的新特性,官方 Javadoc 中有详细示例。
📦 Maven 依赖
(同样为 kafka-clients)
xml
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>4.3.1</version>
</dependency>
4️⃣ Streams API ------ 流式处理库
用途 :在 Kafka 内部完成 实时流处理 ,将一个或多个输入 Topic 的数据转换为输出 Topic 的数据,无需额外计算框架。
典型场景:实时聚合、过滤、连接(Join)、窗口计算、事件时间处理等。
🔧 核心原理
- 基于 Kafka 自身 的持久化存储,利用 状态存储(State Store) 维护中间结果(如窗口聚合)。
- 提供 DSL(领域特定语言) 和 Processor API(底层处理接口),灵活构建处理拓扑(Topology)。
- 天然的 容错 机制:状态变更会写入变更日志 Topic,故障时可重建。
- 以 事件时间(Event Time) 为核心,支持乱序数据的迟到处理和水位线(Watermark)概念(通过
TimestampExtractor和窗口操作实现)。
📦 Maven 依赖
xml
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams</artifactId>
<version>4.3.1</version>
</dependency>
⚠️ 对于 Scala 用户:
官方提供了 kafka-streams-scala 库,但自 Kafka 4.3 起已弃用,将在 5.0 移除。建议直接使用 Java API 或通过 Scala 互操作调用。
xml
<!-- ❌ 已弃用,请勿用于新项目 -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams-scala_2.13</artifactId>
<version>4.3.1</version>
</dependency>
5️⃣ Connect API ------ 数据管道连接器
用途 :实现 可复用的连接器(Connector) ,用于从外部系统(Source)拉取 数据进入 Kafka,或从 Kafka 推送 数据到外部系统(Sink)。
典型场景:将数据库(MySQL、MongoDB)的数据实时同步到 Kafka,或将 Kafka 数据写入 Elasticsearch、HDFS 等。
🔧 核心原理
- Connect 运行在 独立(Standalone) 或 分布式(Distributed) 模式下,提供 REST API 管理连接器。
- Source Connector :监听外部系统变化(如 CDC),将记录转换为
SourceRecord并写入指定 Topic。 - Sink Connector:从 Topic 消费数据,转换为目标系统格式并写入。
- 内置 偏移量管理,保证至少一次语义。
- 绝大多数用户可直接使用 预构建的连接器(如 Debezium、JDBC、Elasticsearch 等),无需编码。
📦 Maven 依赖(开发自定义连接器时)
通常需要引入 kafka-clients 和 connect-api,但官方文档未单独列出,可参考:
xml
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>connect-api</artifactId>
<version>4.3.1</version>
</dependency>
6️⃣ Admin API ------ 集群管理利器
用途 :编程方式管理 Kafka 集群中的各类资源:Topic(创建/删除/修改配置) 、Broker(查询状态) 、ACL(权限控制) 、分区重分配 等。
典型场景:自动化运维工具、控制台系统、动态扩缩容脚本。
🔧 核心原理
- 通过
AdminClient实例发送请求到 Broker 的 控制器(Controller),执行元数据变更。 - 所有操作都是 异步 的,返回
Future或KafkaFuture,便于并发执行。 - 支持 幂等 操作,例如创建 Topic 时若已存在且配置一致,则不报错。
- 可查询集群的元数据(如分区 Leader 分布、ISR 状态),用于监控和诊断。
📦 Maven 依赖
xml
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>4.3.1</version>
</dependency>
🧩 一览表
| API 名称 | 核心职责 | 适用场景 | Maven 构件 | 是否依赖外部框架 |
|---|---|---|---|---|
| Producer | 发送数据到 Topic | 日志、事件上报 | kafka-clients |
否 |
| Consumer | 从 Topic 读取数据 | 实时消费、监控 | kafka-clients |
否 |
| Share Consumer | 协同消费,细粒度确认 | 大规模并行任务 | kafka-clients |
否 |
| Streams | 流式处理(转换/聚合) | 实时计算、ETL | kafka-streams |
否(内置状态存储) |
| Connect | 构建数据管道连接器 | 系统间数据同步 | kafka-clients + connect-api |
可无编码使用预置连接器 |
| Admin | 管理和监控集群资源 | 运维自动化、控制台 | kafka-clients |
否 |
🌟 如何选择?
- 若你只需 发送/接收 消息 → Producer + Consumer。
- 若需要 多个消费者协同且避免重平衡 → Share Consumer。
- 若要做 实时数据计算(如聚合、关联) → Streams API(最强悍、最轻量)。
- 若要在 Kafka 与外部系统间搬数据 → Connect API(或直接使用现成连接器)。
- 若需要 自动化集群操作 → Admin API。
🔗 总结
Kafka 的六大 API 覆盖了消息生命周期的方方面面,从 生产、消费、协同消费 ,到 流处理 、数据管道 和 集群管理,形成了一个强大的生态基石。无论是构建微服务、数据湖,还是实时数仓,Kafka 都能提供优雅的解决方案。
💡 提示 :所有 Java API 的详细用法,请参考官方 Javadoc(链接)中的示例代码。