30 Mins

1. Foundations of Apache Airflow

Architectural Core: DAGs and Tasks

Core Concepts

At the heart of Apache Airflow is the Directed Acyclic Graph, or DAG. A DAG represents a collection of tasks you want to run, organized in a way that reflects their relationships and dependencies. It is directed because each task has a defined flow, and acyclic because loops are forbidden, preventing infinite execution cycles. The topology of your data pipeline can be modeled as a graph $G = (V, E)$, where vertices $V$ represent tasks and edges $E$ represent dependencies.

Tasks are the fundamental units of execution. A Task Instance represents a specific run of a task, associated with a particular execution date. While a DAG defines the 'what' and 'when', Operators define the 'how'. An Operator describes a single, atomic task, such as executing a Bash script, querying a database, or calling an API. Executors then handle the physical work of running these tasks, serving as the interface between your logical DAG and your infrastructure's compute resources.

Operator Note: Always prefer specific operators (e.g., PostgresOperator) over generic PythonOperator or BashOperator to leverage built-in retry logic and connection management.
Python 3
from airflow import DAG
from airflow.operators.bash import BashOperator
from datetime import datetime

# Define DAG context
with DAG('example_pipeline', start_date=datetime(2023, 1, 1), schedule_interval='@daily') as dag:
    # Define a task using an operator
    task_1 = BashOperator(task_id='print_date', bash_command='date')
    task_2 = BashOperator(task_id='echo_msg', bash_command='echo "Pipeline complete"')
    
    # Set dependencies
    task_1 >> task_2

Developing and Testing DAGs

Workflow Development

Writing a DAG in Airflow involves defining Python code that Airflow interprets to generate the task execution graph. By using the 'with' statement, you define a context manager that automatically assigns tasks to the correct DAG instance. This approach improves readability and reduces the overhead of passing the dag argument to every operator constructor.

Before deploying to a production environment, utilize the Airflow CLI for validation. The command 'airflow dags list' helps ensure your DAG is correctly parsed by the scheduler. Additionally, 'airflow tasks test [dag_id] [task_id] [execution_date]' allows you to trigger a single task locally without affecting the state of other tasks or requiring a functional scheduler, which is invaluable for debugging configuration issues or connectivity strings.

Operator Note: Never use the 'test' command to backfill data in production; it ignores dependencies and only executes the specific task selected.
Bash
# Validate DAG file syntax
airflow dags list

# Trigger a single task execution manually for testing
airflow tasks test example_pipeline print_date 2023-01-01
DAG Lineage Graph Visualization
1. PythonOperator
extract_data
2. PythonOperator
transform_data
3. BranchOperator
load_router
DAG Lineage Graph Visualization
1. PythonOperator
extract_data
2. PythonOperator
transform_data
3. BranchOperator
load_router

Knowledge Check

Ready to test your understanding of 1. Foundations of Apache Airflow?

Back