使用 Airflow 构建数据流水线
Volker Janz
Senior Developer Advocate at Astronomer

from airflow.sdk import dag, task, task_group @task_group( group_id="ingest_orders", default_args={"retries": 3}, )def process_orders(): @task def extract_orders(): return [{"id": 1, "amount": 99.99}] @task def transform_orders(orders): return [{"id": o["id"], "total": o["amount"] * 1.08} for o in orders] return transform_orders(extract_orders())
group_id 设置自定义标识符,默认取函数名default_args 作用于组内所有任务,避免重复配置代码未使用任务组

使用任务组

@task_group(group_display_name="Process All")
def process_all():
@task_group(group_display_name="Ingest Orders")
def orders():
return transform(extract())
@task_group(group_display_name="Process Returns")
def returns():
return transform(extract())
return {
"orders": orders(),
"returns": returns(),
}
group_display_name 在 UI 中设置可读标签(支持表情)@task_group def process_source(source_name, source_path): @task def extract(): return read_data(source_path) @task def transform(data): return clean_data(data) return transform(extract())# Reuse the same pattern for different sources orders = process_source("orders", "/data/orders.csv") returns = process_source("returns", "/data/returns.csv") events = process_source("events", "/data/events.csv")

group_id 作为清晰的程序化名称,用 group_display_name 作为UI 标签default_args 共享如重试等配置到组内所有任务使用 Airflow 构建数据流水线