airflow-dag-patterns
它能做什么
这个工具可以帮你为 Apache Airflow 构建自动化工作流(称为 DAG)。它会讲解组织任务的最佳实践,确保任务正确运行,并且便于管理。
适用场景
- ✓需要创建一个自动处理或转移数据的流程时。
- ✓需要让一组任务按特定顺序运行时。
- ✓需要设置定期重复执行的任务时。
技能链接
https://github.com/wshobson/agents/blob/main/plugins/data-engineering/skills/airflow-dag-patterns/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我怎样创建一个工作流,每天早上 6 点下载一个文件,然后再处理它?”
每周 5 个新技能,1 分钟读完。
工作原理
这个工具提供模板和代码示例,展示如何定义任务、任务之间的依赖关系以及重要的设置。它遵循一些原则,例如确保任务可以多次运行而不出问题(幂等性),以及每个任务都有清晰的明确结果(原子性)。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Apache Airflow DAG Patterns
Production-ready patterns for Apache Airflow including DAG design, operators, sensors, testing, and deployment strategies.
When to Use This Skill
- Creating data pipeline orchestration with Airflow
- Designing DAG structures and dependencies
- Implementing custom operators and sensors
- Testing Airflow DAGs locally
- Setting up Airflow in production
- Debugging failed DAG runs
Core Concepts
1. DAG Design Principles
| Principle | Description |
|---|---|
| Idempotent | Running twice produces same result |
| Atomic | Tasks succeed or fail completely |
| Incremental | Process only new/changed data |
| Observable | Logs, metrics, alerts at every step |
2. Task Dependencies
# Linear
task1 >> task2 >> task3
# Fan-out
task1 >> [task2, task3, task4]
# Fan-in
[task1, task2, task3] >> task4
# Complex
task1 >> task2 >> task4
task1 >> task3 >> task4
Quick Start
# dags/example_dag.py
from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.python import PythonOperator
from airflow.operators.empty import EmptyOperator
default_args = {
'owner': 'data-team',
'depends_on_past': False,
'email_on_failure': True,
'email_on_retry': False,
'retries': 3,
'retry_delay': timedelta(minutes=5),
'retry_exponential_backoff': True,
'max_retry_delay': timedelta(hours=1),
}
with DAG(
dag_id='example_etl',
default_args=default_args,
description='Example ETL pipeline',
schedule='0 6 * * *', # Daily at 6 AM
start_date=datetime(2024, 1, 1),
catchup=False,
tags=['etl', 'example'],
max_active_runs=1,
) as dag:
start = EmptyOperator(task_id='start')
def extract_data(**context):
execution_date = context['ds']
# Extract logic here
return {'records': 1000}
extract = PythonOperator(
task_id='extract',
python_callable=extract_data,
)
end = EmptyOperator(task_id='end')
start >> extract >> end
Detailed patterns and worked examples
Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.
Best Practices
Do's
- Use TaskFlow API - Cleaner code, automatic XCom
- Set timeouts - Prevent zombie tasks
- Use
mode='reschedule'- For sensors, free up workers - Test DAGs - Unit tests and integration tests
- Idempotent tasks - Safe to retry
Don'ts
- Don't use
depends_on_past=True- Creates bottlenecks - Don't hardcode dates - Use
{{ ds }}macros - Don't use global state - Tasks should be stateless
- Don't skip catchup blindly - Understand implications
- Don't put heavy logic in DAG file - Import from modules
常见问题
什么是 DAG?
DAG 是 Directed Acyclic Graph(有向无环图)的缩写。你可以把它理解为一张图,展示需要完成的任务以及它们应该发生的顺序,并且不会形成循环(也就是说,不会绕回起点)。
为什么任务需要具备“幂等性”(idempotent)?
幂等性指的是连续多次运行同一个任务,与只运行一次的结果相同。这对可靠性至关重要,因为如果任务失败并需要重新执行,你可以确保最终结果不会被改变。
什么是“算子”(operators)和“传感器”(sensors)?
算子是 DAG 的基本组成部分,代表要执行的一个操作(比如运行一个 Python 脚本)。传感器是一种特殊的算子,会先等待某个条件出现再继续执行(比如等待某个文件到达)。
详情
作者的原始描述
Build production Apache Airflow DAGs with best practices for operators, sensors, testing, and deployment. Use when creating data pipelines, orchestrating workflows, or scheduling batch jobs.
repo: wshobson/agents
热度
- GitHub 星标
- 41k
- 排名
- #6669
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …