Airflow DAG Orchestrator は何をしますか?
適切な依存関係管理、SLA、監視を備えた信頼性の高いデータパイプラインオーケストレーションのためのApache Airflow DAGを設計・運用します。
アクティベーショントリガー
アクティブにする条件: "Airflow", "DAG", "operator", "sensor", "scheduler", "task dependency", "SLA", "backfill", "XCom", "TaskFlow API", "MWAA", "Cloud Composer"
対象外: dbtモデル実行 → dbt-analytics-engineer (ただし、Airflowでdbtをトリガーすることは可能) | ストリーム処理 → streaming-pipeline-architect | ワークフローエンジン (Temporal) → distributed-transaction-manager
クイックスタート
- DAGを定義 — TaskFlow API(
@dag、@taskデコレーター)を使用してPythonネイティブなDAGを作成 - スケジュールを設定 — cronまたはタイムテーブルを使用。バックフィルを意図しない限り
catchup=Falseを設定 - リトライを設定 — すべてのタスクに
retries=2、retry_delay=timedelta(minutes=5)を設定 - SLAを追加 — クリティカルパスのタスクに
sla=timedelta(hours=2)を設定 - ローカルでテスト — デプロイ前に
airflow dags test my_dag 2026-01-01を実行
コア機能
| ドメイン | 技術 |
|---|---|
| Airflow | Apache Airflow 2.10+, MWAA, Cloud Composer 3 |
| オペレーター | BashOperator, PythonOperator, KubernetesPodOperator |
| プロバイダー | apache-airflow-providers-{snowflake, google, aws, dbt-cloud} |
| エグゼキューター | CeleryExecutor, KubernetesExecutor, LocalExecutor |
| 監視 | SLAミス、タスク実行時間、Airflowメトリクス → Prometheus |
アーキテクチャパターン
TaskFlow API DAG
from airflow.decorators import dag, task
from datetime import datetime, timedelta
@dag(
schedule="0 6 * * *", # 毎日6:00 UTC
start_date=datetime(2026, 1, 1),
catchup=False,
default_args={
"retries": 2,
"retry_delay": timedelta(minutes=5),
"sla": timedelta(hours=2),
},
tags=["finance", "daily"],
)
def daily_revenue_pipeline():
@task()
def extract_payments() -> dict:
"""Stripe APIから抽出"""
data = stripe_client.list_payments(date=today())
return {"count": len(data), "path": "s3://raw/payments/"}
@task()
def extract_orders() -> dict:
"""Shopify APIから抽出"""
data = shopify_client.list_orders(date=today())
return {"count": len(data), "path": "s3://raw/orders/"}
@task()
def transform(payments: dict, orders: dict) -> str:
"""DuckDBで結合・変換"""
result_path = run_duckdb_transform(payments["path"], orders["path"])
return result_path
@task()
def load(path: str):
"""Snowflakeにロード"""
snowflake_copy_into("fct_revenue", path)
# 関数呼び出しで依存関係を定義
payments = extract_payments()
orders = extract_orders()
transformed = transform(payments, orders)
load(transformed)
daily_revenue_pipeline()
動的タスクマッピング (ファンアウト/ファンイン)
@task()
def get_partitions() -> list[str]:
return ["2026-01-01", "2026-01-02", "2026-01-03"]
@task()
def process_partition(partition_date: str) -> dict:
"""パーティションごとに並列実行"""
return {"date": partition_date, "rows": process(partition_date)}
@task()
def aggregate(results: list[dict]):
"""ファンイン: すべてのパーティション結果を受け取る"""
total = sum(r["rows"] for r in results)
log.info(f"合計 {total} 行を処理しました")
# process_partition をすべてのパーティションに動的にマッピング
partitions = get_partitions()
results = process_partition.expand(partition_date=partitions)
aggregate(results)
dbt + Airflow 統合
from airflow.operators.bash import BashOperator
from cosmos import DbtDag, ProjectConfig, ProfileConfig
# オプション1: cosmos (推奨)
dbt_dag = DbtDag(
project_config=ProjectConfig("/opt/airflow/dbt/"),
profile_config=ProfileConfig(
profile_name="default",
target_name="prod",
),
schedule="@daily",
dag_id="dbt_daily",
)
# オプション2: BashOperator (シンプル)
dbt_run = BashOperator(
task_id="dbt_run",
bash_command="cd /opt/airflow/dbt && dbt build --select tag:daily",
)
アンチパターン
- Fat tasks (過度に大きなタスク) — タスクはアトミックな単位であるべき。1つのタスクに抽出+変換+ロードを含めてはいけません
- 大規模データにXComを使用 — XComはメタデータ(パス、カウント)用であり、データ自体はS3/GCS経由で受け渡し。XComのpickleは使わない
- 誤ったcatchup=True — バックフィルが必要でない限り
catchup=Falseを設定。設定しない場合、Airflowはすべての未実行インターバルを実行します - リトライなし — 一時的な障害はよく発生する。常に
retries >= 1と遅延を設定 - DAGファイルのトップレベルコード — DAGファイルは30秒ごとに解析される。モジュールレベルでの重いインポートやAPI呼び出しはスケジューラーを遅くします
品質チェックリスト
- PythonネイティブなDAGにはTaskFlow APIを使用(従来のオペレーターチェーンは使用しない)
- バックフィルを意図しない限り
catchup=Falseを設定 - すべてのタスクにリトライを設定(最低1回)
- クリティカルパスのタスクにSLAを設定
- XComは参照(S3パス)を渡し、データペイロードは渡さない
- ファンアウト並列処理に動的タスクマッピングを使用
- デプロイ前に
airflow dags testでDAGをローカルテスト - タスクの冪等性: タスクを再実行しても同じ結果が得られる
- タスク失敗とSLAミスに対するアラート設定
- Airflow UIでフィルタリングするためのDAGタグを適用