Community研究与数据分析github.com

Unknown-333/authoring-airflow-dags

编写生产级 Apache Airflow DAG,使用 TaskFlow API 实现幂等任务、正确调度与回填、重试/SLA、连接/变量,并避免顶层代码。适用于创建或审查 Airflow DAG、调度管道、编排任务依赖、配置重试/回填,或修复非幂等任务。

authoring-airflow-dags 是什么?

该技能指导开发者编写生产级 Apache Airflow DAG,重点使用 TaskFlow API 实现幂等性任务,确保任务在重跑时结果一致,并避免使用 `datetime.now()` 而依赖数据间隔。它强调正确配置调度与回填,默认关闭 `catchup` 以防止集群过载,同时设置重试、重试延迟和 SLA 以处理临时故障。技能还涵盖常见陷阱,如顶层代码(数据库/API 调用或大导入)会拖慢调度器,以及通过 XCom 传递小数据、通过存储服务(如 S3/GCS)传递大数据,避免元数据库膨胀。支持平台包括 Airflow 2.0+ 的 TaskFlow API,适合在创建或审查 DAG 时使用,但不适用于诊断已崩溃的运行中 DAG。

兼容平台~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 传递兆字节。

使用连接/变量 处理密钥和配置(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.

相关技能