Travailler avec les modèles

Introduction à Apache Airflow en Python

Mike Metzger

Data Engineer

Qu’est-ce qu’un modèle ?

Les modèles :

  • Permettent de remplacer des informations lors de l’exécution d’un DAG
  • Offrent plus de flexibilité pour définir les tâches
  • Sont créés avec le langage de gabarits Jinja
Introduction à Apache Airflow en Python

Exemple sans modèle avec BashOperator

Créer une tâche pour afficher une liste de fichiers :

t1 = BashOperator(
       task_id='first_task',
       bash_command='echo "Reading file1.txt"',
       dag=dag)

t2 = BashOperator( task_id='second_task', bash_command='echo "Reading file2.txt"', dag=dag)
Introduction à Apache Airflow en Python

Exemple avec modèle dans BashOperator

templated_command="""
  echo "Reading {{ params.filename }}"
"""

t1 = BashOperator(task_id='template_task', bash_command=templated_command, params={'filename': 'file1.txt'}, dag=example_dag)

Sortie :

Reading file1.txt
Introduction à Apache Airflow en Python

Exemple avec modèle (suite)

templated_command="""
  echo "Reading {{ params.filename }}"
"""

t1 = BashOperator(task_id='template_task', bash_command=templated_command, params={'filename': 'file1.txt'}, dag=example_dag)
t2 = BashOperator(task_id='template_task', bash_command=templated_command, params={'filename': 'file2.txt'}, dag=example_dag)
Introduction à Apache Airflow en Python

Passons à la pratique !

Introduction à Apache Airflow en Python

Preparing Video For Download...