Airflow First DAG
A first DAG turns the ideas you learned into something you can see running. You will build a tiny three-step pipeline, save it, watch it in the UI, and read its logs. The pipeline prints messages only, so nothing can break your system.
The Plan: A Tea Shop Pipeline
Picture a tea shop that follows three steps every morning: collect orders, brew the tea, and serve the cups. Each step needs the one before it. The DAG mirrors that routine.
┌───────────────┐ ┌─────────────┐ ┌─────────────┐ │ collect_orders│ ──► │ brew_tea │ ──► │ serve_tea │ └───────────────┘ └─────────────┘ └─────────────┘
Step 1: Create the File
Create a file named tea_shop_dag.py inside your dags folder. Run airflow config get-value core dags_folder to see where that folder lives.
Step 2: Write the Code
from datetime import datetime
from airflow import DAG
from airflow.operators.python import PythonOperator
def collect_orders():
print("Collected 12 tea orders")
def brew_tea():
print("Brewing 12 cups of tea")
def serve_tea():
print("Served 12 cups of tea")
with DAG(
dag_id="tea_shop",
start_date=datetime(2024, 1, 1),
schedule="@daily",
catchup=False,
tags=["beginner"],
) as dag:
collect = PythonOperator(task_id="collect_orders", python_callable=collect_orders)
brew = PythonOperator(task_id="brew_tea", python_callable=brew_tea)
serve = PythonOperator(task_id="serve_tea", python_callable=serve_tea)
collect >> brew >> serve
Understanding Each Part
Imports
The imports bring in the DAG class, the PythonOperator, and a date helper.
Task Functions
Each function holds the work of one step. Real pipelines place database queries or file operations here.
DAG Settings
| Setting | Meaning |
|---|---|
dag_id | Unique name shown in the UI |
start_date | Earliest date the schedule applies to |
schedule | How often the DAG runs |
catchup=False | Skips missed past runs |
tags | Labels that help you filter in the UI |
Dependencies
The line collect >> brew >> serve sets the order. The arrow points from the earlier task to the later one.
Step 3: See the DAG in the UI
Open the Airflow page and look for tea_shop in the DAG list. The scheduler needs up to a minute to notice a new file. A missing DAG usually means an error in the code. Run airflow dags list-import-errors to find it.
Step 4: Run It
- Switch the toggle on the left of the DAG name to unpause it.
- Click the play button and choose Trigger DAG.
- Open the Grid view and watch the squares change color.
Grid view colors ──────────────── light green = running dark green = success red = failed grey = waiting
Step 5: Read the Logs
Click a green square, choose Logs, and read the output. The log for brew_tea shows the line "Brewing 12 cups of tea". Logs prove the task ran and show the exact error when something fails.
Step 6: Test from the Terminal
airflow tasks test tea_shop brew_tea 2024-01-15
The command runs one task and prints the output right in your terminal.
Common First-DAG Problems
| Problem | Likely Cause |
|---|---|
| DAG missing from the list | Syntax error or wrong folder |
| DAG never starts | DAG is paused, or start_date lies in the future |
| Task fails at once | Typo inside the task function |
Try It Yourself
- Add a fourth task named
clean_cupsthat runs afterserve_tea. - Change the schedule to
@hourlyand watch new runs appear. - Break a function on purpose and find the error in the logs.
Key Takeaways
- A DAG file is plain Python in the dags folder.
- Operators wrap your functions into tasks.
- The
>>symbol defines order. - The UI and logs confirm that everything works.
