Community研究與資料分析github.com

Unknown-333/authoring-airflow-dags

撰寫生產級 Apache Airflow DAG,使用 TaskFlow API — 包含冪等任務、正確的排程與補跑、重試/SLA、連線/變數,以及避免頂層程式碼。

authoring-airflow-dags 是什麼?

此技能專注於指導開發者使用 Apache Airflow 的 TaskFlow API 設計與撰寫生產級 DAG。其核心目標是確保每個任務具有冪等性(Idempotent),透過使用資料區間(Data Interval)而非 `datetime.now()` 來避免重跑時產生不一致結果。技能文件詳細說明了如何避免在模組頂層放置耗時匯入或 API 呼叫,以防影響排程器解析;同時強調正確設定排程、補跑(Catchup)、重試次數(Retries)與服務等級協議(SLA)。支援的平台為 Apache Airflow,適用於建立新 DAG、重構既有管道、設定任務依賴與資料傳遞(XCom)等場景。透過此技能,開發者能寫出更加穩定、可維護且符合最佳實務的 Airflow 工作流程。

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

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

在你喜歡的 AI 中提問

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

說明文件

撰寫 Airflow DAG

使用時機

  • 建立或重構 Airflow DAG 與任務。
  • 設定排程、補跑/回填、重試與 SLA。
  • 在任務間傳遞資料(XCom)或使用連線/變數。
  • 請勿用於診斷正在執行但中斷的 DAG(請使用 debugging-airflow-pipelines)。

工作流程

- [ ] 讓每個任務具備冪等性,並以資料區間為參數
- [ ] 將耗費資源/大量匯入的程式碼放在任務內部,而非模組頂層
- [ ] 刻意設定排程與補跑
- [ ] 設定重試次數、重試延遲與 SLA
- [ ] 使用 TaskFlow 回傳值或 >> 運算子建立相依關係
  1. 冪等任務 — 針對 2026-01-15 區間的任務,無論執行一次或重新執行,都必須產生相同結果。請使用資料區間,而非 datetime.now()。
  2. 避免耗時頂層程式碼 — 排程器會頻繁解析每個 DAG 檔案;在模組頂層進行資料庫呼叫、API 呼叫或大型匯入會拖慢排程,甚至導致解析失敗。請將它們放在任務內部。
  3. 刻意設定補跑 — catchup=True 會從 start_date 開始回補所有錯過的區間;除非有意為之,否則預設為 False。
  4. 重試與 SLA — 暫時性失敗是正常的;設定 retries 與 retry_delay;使用 SLA/警示來處理延遲。

模式

TaskFlow DAG,具冪等性且依賴清晰:

from airflow.decorators import dag, task
import pendulum

@dag(
    schedule="@daily",
    start_date=pendulum.datetime(2026, 1, 1, tz="UTC"),
    catchup=False,
    default_args={"retries": 3, "retry_delay": pendulum.duration(minutes=5)},
    tags=["orders"],
)
def orders_pipeline():

    @task
    def extract(data_interval_start=None):
        # 使用區間而非 now(),確保重新執行時結果一致。
        return fetch_orders(day=data_interval_start.date())

    @task
    def load(rows):
        # 先刪除再插入分割區 -> 重試時具冪等性。
        overwrite_partition("fct_orders", rows)

    load(extract())

orders_pipeline()

透過 XCom 傳遞小量資料(回傳值);大量資料則透過儲存服務傳遞 — 寫入 S3/GCS/資料倉儲,並傳遞路徑/金鑰,永遠不要透過 XCom 傳遞數 MB 的資料。

使用連線/變數 來處理機密資訊與設定(BaseHook.get_connection、Variable.get),絕不硬編碼憑證。

常見陷阱

  • 頂層 API/資料庫呼叫或大量匯入 — 拖慢排程器,可能導致整個部署環境的 DAG 解析失敗。
  • 任務內使用 datetime.now() — 破壞冪等性與補跑功能;請使用 data_interval_start/_end。
  • 非預期的 catchup=True — 初次部署時會用歷史執行記錄淹沒叢集。
  • 透過 XCom 傳遞大型負載 — 使中繼資料庫膨脹;請傳遞參考。
  • 動態 start_date(例如 days_ago)— 導致排程不確定;請使用固定時間戳。
  • 單一巨大任務 — 將提取/轉換/載入拆分,以便更精細地進行重試。

參考資料

Individual skills in this repo

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

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/building-kafka-consumers

Build reliable Apache Kafka consumers and producers — consumer groups and partition assignment, offset commit strategy, at-least-once vs exactly-once, idempotent/transactional producers, rebalancing, and dead-letter handling. Use when writing Kafka consumers/producers, configuring offset commits or consumer groups, tuning throughput, or handling rebalances and poison messages.

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.

相關技能