Se ti stai chiedendo come creare un DAG in Apache Airflow, sei nel posto giusto. Ti guiderò passo passo nella creazione del tuo primo flusso di lavoro.
Cos’è un DAG
Un DAG (Directed Acyclic Graph) rappresenta un flusso di lavoro: i task da eseguire, le dipendenze tra essi, lo schedule e i parametri di configurazione.
Struttura base
from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.python import PythonOperator
from airflow.operators.bash import BashOperator
default_args = {
'owner': 'stanislao',
'retries': 1,
'retry_delay': timedelta(minutes=5),
}
with DAG(
dag_id='il_mio_primo_dag',
default_args=default_args,
schedule_interval='@daily',
start_date=datetime(2024, 1, 1),
catchup=False,
) as dag:
stampa_data = BashOperator(
task_id='stampa_data',
bash_command='echo "$(date)"',
)
def estrai_dati(**context):
print(f"Estrazione per {context['ds']}")
return {'record_count': 100}
estrazione = PythonOperator(
task_id='estrai_dati',
python_callable=estrai_dati,
)
stampa_data >> estrazioneSalvare e attivare
- Salva il file in
~/airflow/dags/ - Airflow rileva automaticamente il file
- Attiva il DAG dall’interfaccia web
Testare il DAG
airflow dags list
airflow tasks test il_mio_primo_dag stampa_data 2024-01-01Best Practice
- Usa nomi descrittivi
- Configura i default_args
- Mantieni i task idempotenti
- Testa prima di deployare
Ora hai tutti gli strumenti per creare il tuo primo DAG!