Diramazione

Introduzione ad Apache Airflow in Python

Mike Metzger

Data Engineer

Diramazione

  • Fornisce logica condizionale (es. if -> then per i task)
  • Usando @task.branch

  • Esegue una funzione Python che restituisce l’id del prossimo task (o una lista di id) da seguire

Introduzione ad Apache Airflow in Python

Esempio di diramazione

@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'
Introduzione ad Apache Airflow in Python

Esempio di diramazione

@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
Introduzione ad Apache Airflow in Python

Vista grafico diramazione

Vista grafico di Airflow di un Dag con diramazione e due percorsi a valle

Introduzione ad Apache Airflow in Python

Diramazione Mesi di fine trimestre

Vista grafico di Airflow con attività di fine trimestre eseguite e attività mensili saltate

Introduzione ad Apache Airflow in Python

Diramazione Mesi normali

Vista grafico di Airflow con attività mensili eseguite e attività di fine trimestre saltate

Introduzione ad Apache Airflow in Python

Variabili di data

  • ds - Data logica con trattini YYYY-MM-DD
  • ds_nodash - Data logica senza trattini YYYYMMDD
  • prev_data_interval_start_success - Data dell’ultimo Dag riuscito
  • Molte altre - vedi documentazione variabili Airflow
1 https://airflow.apache.org/docs/apache-airflow/stable/templates-ref.html#templates-variables
Introduzione ad Apache Airflow in Python

Ayo berlatih!

Introduzione ad Apache Airflow in Python

Preparing Video For Download...