Introduction
Airflow helps you run and manage data tasks in order. It makes sure each step happens at the right time.
Airflow helps you run and manage data tasks in order. It makes sure each step happens at the right time.
from airflow import DAG from airflow.operators.bash import BashOperator from datetime import datetime with DAG('my_pipeline', start_date=datetime(2024, 1, 1), schedule_interval='@daily') as dag: task1 = BashOperator( task_id='run_spark_job', bash_command='spark-submit --class my.main.Class /path/to/app.jar' ) task2 = BashOperator( task_id='cleanup', bash_command='rm -rf /tmp/data' ) task1 >> task2
The DAG defines the workflow and schedule.
Tasks run in order using the '>>' operator.
from airflow import DAG from airflow.operators.bash import BashOperator from datetime import datetime with DAG('daily_spark_job', start_date=datetime(2024, 1, 1), schedule_interval='@daily') as dag: run_spark = BashOperator( task_id='run_spark', bash_command='spark-submit --master local /path/to/job.py' )
task1 = BashOperator(
task_id='extract_data',
bash_command='python extract.py'
)
task2 = BashOperator(
task_id='transform_data',
bash_command='python transform.py'
)
task1 >> task2This code defines two tasks: one simulates running a Spark job, the other cleans up files. They run in order.
from airflow import DAG from airflow.operators.bash import BashOperator from datetime import datetime with DAG('spark_pipeline', start_date=datetime(2024, 1, 1), schedule_interval='@daily') as dag: run_spark = BashOperator( task_id='run_spark_job', bash_command='echo Running Spark job... && sleep 1 && echo Spark job done' ) cleanup = BashOperator( task_id='cleanup_temp', bash_command='echo Cleaning up temporary files... && sleep 1 && echo Cleanup done' ) run_spark >> cleanup # Simulate running tasks by calling their bash_command manually print("Task run_spark_job output:") print("Running Spark job...\nSpark job done") print("\nTask cleanup_temp output:") print("Cleaning up temporary files...\nCleanup done")
Airflow uses DAGs (Directed Acyclic Graphs) to manage task order.
Each task can be a script, command, or Spark job.
You can see task status in Airflow's web interface.
Airflow helps automate and order data tasks.
Define tasks and their order using DAGs and operators.
It works well with Spark jobs and other scripts.
spark-submit?spark-submit.task1 = BashOperator(task_id='task1', bash_command='spark-submit job1.py', dag=dag) task2 = BashOperator(task_id='task2', bash_command='spark-submit job2.py', dag=dag) task3 = BashOperator(task_id='task3', bash_command='spark-submit job3.py', dag=dag) task1 >> task2 >> task3
>> means 'runs before', so task1 runs before task2, and task2 before task3.task1 = BashOperator(task_id='task1', bash_command='spark-submit job1.py', dag=dag) task2 = BashOperator(task_id='task1', bash_command='spark-submit job2.py', dag=dag) task1 >> task2
job1 = BashOperator(task_id='job1', bash_command='spark-submit job1.py', dag=dag) job2 = BashOperator(task_id='job2', bash_command='spark-submit job2.py', dag=dag) job3 = BashOperator(task_id='job3', bash_command='spark-submit job3.py', dag=dag) job1.set_downstream(job2) job2.set_downstream(job3)uses set_downstream correctly: job1 -> job2 -> job3. The other snippets reverse or mix the order incorrectly.
Pass the test and this pattern moves unknown