Community研究與資料分析github.com

建立 Kafka 消費者

構建可靠的 Apache Kafka 消費者和生產者—消費者群組與分區分配、位移提交策略、至少一次 vs 恰好一次、冪等/交易生產者、再平衡、以及死信處理。在撰寫 Kafka 消費者/生產者、配置位移提交或消費者群組、調整吞吐量、或處理再平衡與毒藥訊息時使用。

建立 Kafka 消費者 是什麼?

此技能提供建置可靠 Apache Kafka 消費者與生產者的完整指南,涵蓋消費者群組、分區分配、位移提交策略、傳遞保證(至少一次 vs 恰好一次)、冪等與交易生產者、再平衡處理,以及死信訊息路由。主要支援 Kafka 原生用戶端(如 Java、Python、Go 等),並特別強調在手動位移提交、冪等接收端與死信佇列等常見模式上的實作。技能包含詳細的工作流程、程式碼範例(如 Python 示範),以及常見陷阱(如自動提交搭配慢速處理、消費者過多於分區、再平衡超時、缺少死信路徑)。它幫助開發者避免資料遺失或重複,確保在高吞吐量與故障發生時仍維持資料完整性。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/Unknown-333/awesome-data-engineering-skills/tree/main/skills/building-kafka-consumers

Installed? Explore more 研究與資料分析 skills: affaan-m/uspto-database, affaan-m/scholar-evaluation, affaan-m/literature-review · View all 6 →

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

建立 Kafka 消費者 是做什麼的?

使用時機

  • 撰寫或除錯 Kafka 消費者/生產者。
  • 選擇位移提交策略與傳遞保證。
  • 調整消費者群組平行度、再平衡或死信處理。
  • 不適用於串流處理/視窗化(請使用 processing-streaming-data)。

工作流程

- [ ] 將分區大小調整至目標平行度(消費者數量 <= 分區數量)
- [ ] 在成功處理**後**提交位移
- [ ] 讓接收端成為冪等(依事件鍵進行 upsert)
- [ ] 處理再平衡(在回收時提交,避免長時間輪詢間隔)
- [ ] 將毒藥訊息路由至死信主題
  1. 分區限制了平行度。 消費者群組只能擴展到分區數量;多餘的消費者會閒置。請根據尖峰吞吐量選擇分區數。
  2. 處理後再提交。 只有在工作持久完成後才提交位移,不要提前提交——提前提交會在崩潰時遺失訊息。
  3. 冪等接收端。 至少一次意味著重試時會產生重複;透過穩定的事件鍵進行 upsert,讓重新處理不會造成影響。
  4. 再平衡一定會發生。 在分區回收時提交位移,並讓 poll() 間隔保持在 max.poll.interval.ms 以下,以免代理程式將消費者踢出。
  5. 毒藥訊息應送往死信主題並附上錯誤資訊,這樣一個壞紀錄就不會阻塞整個分區。

模式

處理後手動提交:

consumer = KafkaConsumer("orders", group_id="etl",
                         enable_auto_commit=False,
                         max_poll_records=500)
for msg in consumer:
    try:
        upsert(process(msg))          # 依鍵值進行冪等處理
        consumer.commit()             # 成功後才提交
    except PoisonError:
        send_to_dlq(msg)
        consumer.commit()             # 跳過壞紀錄

冪等 / 交易生產者 — 設定 enable.idempotence=true(去重複重試),並使用交易實現在主題間進行讀取-處理-寫入的恰好一次語意。

吞吐量調整 — 增加 max.poll.records、fetch.min.bytes,並批次處理;保持處理速度快,以避免再平衡踢除。

常見陷阱

  • 自動提交 + 慢速處理 — 位移在工作完成前就前進;崩潰會丟失訊息。建議在處理後手動提交。
  • 消費者數量多於分區 — 多餘的消費者閒置;請重新分區以擴展。
  • 輪詢之間處理時間過長 — 超過 max.poll.interval.ms 會觸發無限再平衡;請在有限的批次中處理或使用背景工作者。
  • 沒有死信路徑 — 一個毒藥訊息會阻塞整個分區。
  • 依賴恰好一次卻沒有冪等接收端 — 任何至少一次的跳躍都會重新引入重複。

Individual skills in this repo

This repo contains 9 individual skills — each has its own dedicated page.

Unknown-333/authoring-airflow-dags

Write production-grade Apache Airflow DAGs using the TaskFlow API — idempotent tasks, correct scheduling and catchup, retries/SLAs, connections/variables, and avoiding top-level code. Use when creating or reviewing Airflow DAGs, scheduling pipelines, wiring task dependencies, configuring retries/backfills, or fixing non-idempotent tasks.

Unknown-333/building-dagster-assets

Build Dagster pipelines using software-defined assets — asset dependencies, partitions, resources and IO managers, asset checks, and schedules/sensors. Use when creating Dagster assets or jobs, modeling data as assets, adding partitions or backfills, wiring resources/IO managers, or migrating from task-based orchestration to assets.

Unknown-333/building-dbt-models

Build well-structured dbt models — staging/intermediate/marts layers, ref() and source(), materializations, and incremental models with the right strategy. Use when creating or refactoring dbt models, choosing table vs view vs incremental, structuring a dbt project, or writing incremental logic.

Unknown-333/building-feature-pipelines

Build ML feature pipelines and feature stores — point-in-time-correct joins to avoid label leakage, offline/online parity, feature freshness and backfills, and materialization with tools like Feast. Use when engineering features for ML, preventing train/serve skew or data leakage, building a feature store, or backfilling historical features for training.

Unknown-333/building-iceberg-tables

Design and operate Apache Iceberg tables — partitioning and hidden partitioning, partition/schema evolution, snapshots and time travel, compaction and small-file cleanup, and MERGE/upsert for lakehouse tables on Spark, Flink, Trino, or Snowflake. Use when creating or maintaining Iceberg tables, choosing partitioning, evolving schema/partitions, or fixing small-file and metadata bloat.

Unknown-333/building-ingestion-pipelines

Build batch and incremental data ingestion (extract-load) pipelines — full vs incremental extraction, change data capture (CDC), watermarks and high-water marks, API pagination and rate limits, and choosing managed EL tools (Fivetran, Airbyte) vs custom code. Use when ingesting data from databases, APIs, files, or SaaS into a warehouse/lake, or designing incremental extraction and CDC.

Unknown-333/debugging-data-pipelines

Systematically root-cause data pipeline failures and data incidents — job errors, wrong or missing data, duplicates, and freshness misses — by tracing lineage upstream, isolating the failing stage, reconciling against source, and planning a safe fix and backfill. Use when a pipeline fails, numbers look wrong, data is missing or duplicated, a dashboard is stale, or a stakeholder reports a data discrepancy.

Unknown-333/designing-backfills-and-replays

Plan and run safe data backfills and replays — idempotent reprocessing of historical windows, partition-by-partition execution, isolating backfill compute from production, verifying results, and avoiding double-counting or changed history. Use when backfilling a new or fixed model, reprocessing after a bug, replaying events, or loading history for a new pipeline without corrupting existing data.

Unknown-333/designing-data-contracts

Define and enforce data contracts between producers and consumers — explicit schema, semantics, ownership, SLAs, and versioning — to prevent silent upstream changes from breaking downstream pipelines. Use when a producer schema change could break consumers, defining an interface between teams/services and the warehouse, or adding schema enforcement at ingestion.

相關技能