Operadores e Hooks do Airflow

Referência rápida dos principais operadores e hooks do Apache Airflow.

Índice

  1. Operadores Principais
    1. PythonOperator
    2. BashOperator
    3. BranchPythonOperator
  2. Hooks Comuns

Operadores Principais

PythonOperator

Executa uma função Python como uma tarefa do DAG.

1
2
3
4
5
6
7
8
9
10
11
from airflow.operators.python import PythonOperator

def minha_funcao(**context):
    print(f"Executando na data: {context['ds']}")
    return "sucesso"

tarefa = PythonOperator(
    task_id="minha_tarefa",
    python_callable=minha_funcao,
    dag=dag,
)

BashOperator

Executa comandos shell.

1
2
3
4
5
6
7
from airflow.operators.bash import BashOperator

executar_script = BashOperator(
    task_id="executar_script",
    bash_command="python /scripts/processar.py --date ",
    dag=dag,
)

BranchPythonOperator

Permite ramificações condicionais no DAG.

1
2
3
4
5
6
7
8
9
10
11
12
from airflow.operators.python import BranchPythonOperator

def decidir_ramo(**context):
    if context["ds"] == "2024-01-01":
        return "tarefa_especial"
    return "tarefa_normal"

branch = BranchPythonOperator(
    task_id="decidir",
    python_callable=decidir_ramo,
    dag=dag,
)

Hooks Comuns

Hook Provedor Uso
S3Hook apache-airflow-providers-amazon Interações com AWS S3
PostgresHook apache-airflow-providers-postgres Conexão com PostgreSQL
HttpHook apache-airflow-providers-http Chamadas HTTP/REST APIs
GCSHook apache-airflow-providers-google Google Cloud Storage
BigQueryHook apache-airflow-providers-google Google BigQuery

Para sensores, veja Sensores — Poke vs Reschedule.


© BP STRAT. Todos os direitos reservados. readme