分岐

Python で学ぶ Apache Airflow 入門

Mike Metzger

Data Engineer

分岐

  • 条件ロジックを提供(タスクのif→then処理)
  • @task.branch を使用

DNT_CURLY_TAG_2

  • Python関数を実行し、次に実行するタスクID(またはIDのリスト)を返す
Python で学ぶ Apache Airflow 入門

分岐の例

@task.branch
def branch_task(logical_date):
  if int(logical_date.month) % 3 == 0:
    # Months 3 - March, 6 - June, 9 - September, 12 - December
    return 'end_of_quarter_task'
  else:
    # All other months
    return 'regular_monthly_task'
Python で学ぶ Apache Airflow 入門

分岐の例

@task.branch
def branch_task(logical_date):
  if int(logical_date.month) % 3 == 0:
    # Months 3 - March, 6 - June, 9 - September, 12 - December
    return 'end_of_quarter_task'
  else:
    # All other months
    return 'regular_monthly_task'

start_task >> branch_task >> end_of_quarter_task >> end_of_quarter_task2
branch_task >> regular_monthly_task >> regular_monthly_task2
Python で学ぶ Apache Airflow 入門

分岐グラフビュー

2つの下流タスクパスを持つ分岐DAGのAirflowグラフビュー

Python で学ぶ Apache Airflow 入門

分岐:四半期末月

四半期末タスクが実行され、月次タスクがスキップされたAirflowグラフビュー

Python で学ぶ Apache Airflow 入門

分岐:通常月

通常の月次タスクが実行され、四半期末タスクがスキップされたAirflowグラフビュー

Python で学ぶ Apache Airflow 入門

日付変数

  • ds - 論理日付(ダッシュあり)YYYY-MM-DD
  • ds_nodash - 論理日付(ダッシュなし)YYYYMMDD
  • prev_data_interval_start_success - 最後に成功したDAGの実行日
  • その他多数 - Airflow変数のドキュメントを参照
1 https://airflow.apache.org/docs/apache-airflow/stable/templates-ref.html#templates-variables
Python で学ぶ Apache Airflow 入門

練習しましょう!

Python で学ぶ Apache Airflow 入門

Preparing Video For Download...