Trabalhando com templates

Introdução ao Apache Airflow em Python

Mike Metzger

Data Engineer

O que são templates?

Templates:

  • Permitem substituir informações durante a execução do DAG
  • Dão mais flexibilidade ao definir tarefas
  • São criados com a linguagem de template Jinja
Introdução ao Apache Airflow em Python

Exemplo sem template no BashOperator

Crie uma tarefa para exibir uma lista de arquivos:

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)
Introdução ao Apache Airflow em Python

Exemplo com template no BashOperator

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

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

Saída:

Reading file1.txt
Introdução ao Apache Airflow em Python

Exemplo com template no BashOperator (continuação)

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)
Introdução ao Apache Airflow em Python

Vamos praticar!

Introdução ao Apache Airflow em Python

Preparing Video For Download...