Apache Airflow: Understanding DAGs - Comprehensive Study Notes
Introduction to Apache Airflow DAGs
This video dives into the core of Apache Airflow: Directed Acyclic Graphs (DAGs).
It aims to explain what a DAG is, its importance, key components, how to write, load, and trigger DAGs, including examples.
The goal is to equip viewers to write their own Airflow workflows.
What is a DAG in Airflow?
DAG stands for Directed Acyclic Graph.
It represents the entire workflow structure in Airflow.
A DAG is fundamentally a combination of tasks.
Components of a DAG include:
Schedule (when it runs)
Tasks (individual units of work)
Task Dependencies (order of execution)
Trigger Rules (conditions for task execution)
Default Arguments (common parameters for tasks)
Connections
Variables
The DAG defines the execution order and control flow of these components.
Typical DAG Structure and Examples
A typical DAG is visualized as a network of interconnected boxes (tasks) with arrows (dependencies).
Example Workflow Illustration:
Start Task: A dummy operator marking the beginning.
Extract Data: A task to pull initial data.
Extract Master Data: (Assumed to be a parallel or subsequent task to extract data based on context, then dependencies kick in).
Parallel Tasks: After
Extract Master Datacompletes, three tasks (Task 1,Task 2,Task 3) trigger independently and run in parallel.Conditional End Task: Once
Task 1,Task 2, andTask 3are completed, it triggers eitherNotify FailureorJoin Transformbased on their defined trigger rules.
Airflow UI Demonstrations:
Simple Test DAG: Demonstrated in the Airflow UI with
start_task,print_date, andprint_hellotasks, showing parallel execution afterstart_task.Sequential DAG: Illustrated an
extracttransformloadpattern, where each task is dependent on the successful completion of the previous one.start_taskextract_datatransform_dataload_dataend_status(dummy operator to mark completion).
DAG Schedule in UI:
The Airflow UI displays the
schedule_intervalfor each DAG (e.g.,Nonefor manual trigger,Dailyfor daily execution).DAGs can be enabled or disabled; only enabled DAGs will run on their scheduled time.
Key Components of a DAG
When writing a DAG definition file (a Python .py file), several components are defined:
Default Arguments:
A Python dictionary holding common parameters for tasks within the DAG.
Examples:
start_date,retries,retry_delay,email.Purpose: To avoid code repetition across tasks and keep the DAG definition clean and concise.
Tasks:
The individual units of work or operations that make up the workflow.
Examples provided:
start_task(dummy),print_hello_task,print_date_task.
Task Dependencies:
Define the execution order of tasks.
Can establish sequential or parallel execution flows.
Example:
start_taskmust complete beforeprint_hello_taskandprint_date_taskcan run in parallel. This is defined using bitshift operators, e.g.,start_task >> [print_hello_task, print_date_task].
Schedule Interval:
Controls when the DAG runs.
Defined using the
schedule_intervalparameter in the DAG constructor.Can be specified using:
Cron expressions: e.g., `