Branchement

Introduction à Apache Airflow en Python

Mike Metzger

Data Engineer

Branchement

  • Ajoute une logique conditionnelle (ex. if -> then entre tâches)
  • Utilise @task.branch

  • Exécute une fonction Python qui retourne l’ID (ou liste d’ID) de la prochaine tâche

Introduction à Apache Airflow en Python

Exemple de branchement

@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'
Introduction à Apache Airflow en Python

Exemple de branchement

@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
Introduction à Apache Airflow en Python

Vue graphique du branchement

Vue Graph d’Airflow d’un DAG avec embranchements et deux chemins en aval

Introduction à Apache Airflow en Python

Branchement Mois de fin de trimestre

Vue Graph d’Airflow avec les tâches de fin de trimestre exécutées et les tâches mensuelles ignorées

Introduction à Apache Airflow en Python

Branchement Mois ordinaires

Vue Graph d’Airflow avec les tâches mensuelles exécutées et celles de fin de trimestre ignorées

Introduction à Apache Airflow en Python

Variables de date

  • ds - Date logique avec tirets YYYY-MM-DD
  • ds_nodash - Date logique sans tirets YYYYMMDD
  • prev_data_interval_start_success - Date du dernier Dag réussi
  • Et bien d’autres - voir la documentation des variables Airflow
1 https://airflow.apache.org/docs/apache-airflow/stable/templates-ref.html#templates-variables
Introduction à Apache Airflow en Python

Passons à la pratique !

Introduction à Apache Airflow en Python

Preparing Video For Download...