📖 Scenario: You are a data engineer setting up a simple workflow to run daily tasks using Apache Airflow. You want to create a Directed Acyclic Graph (DAG) that defines the order of tasks.
🎯 Goal: Build a basic Airflow DAG with two tasks where the second task runs after the first. This will help you understand how to define workflows in Airflow using DAGs.
📋 What You'll Learn
Create a DAG object with a specific dag_id and schedule
Define two PythonOperator tasks with simple Python functions
Set task dependencies so the second task runs after the first
Print the DAG structure by listing task IDs in order
💡 Why This Matters
🌍 Real World
Airflow DAGs are used to automate and schedule workflows like data pipelines, report generation, and system maintenance tasks.
💼 Career
Understanding DAGs is essential for roles like data engineer, DevOps engineer, and workflow automation specialist who manage task scheduling and orchestration.
Progress0 / 4 steps
1
Create the DAG object
Import DAG from airflow and create a DAG object called simple_dag with dag_id='simple_dag' and schedule_interval='@daily'. Use start_date=datetime(2024, 1, 1) and catchup=False.
Apache Airflow
Hint
Use the DAG class from airflow and pass the parameters exactly as shown.
2
Define two PythonOperator tasks
Import PythonOperator from airflow.operators.python. Define two Python functions called task_one_func and task_two_func that each print a simple message. Then create two tasks called task_one and task_two using PythonOperator with task_id matching the variable names and assign python_callable to the respective functions. Attach both tasks to the simple_dag DAG.
Apache Airflow
Hint
Define simple functions that print messages and use them as python_callable in PythonOperator.
3
Set task dependencies
Set the dependency so that task_two runs after task_one by writing task_one >> task_two.
Apache Airflow
Hint
Use the >> operator to set the order of tasks.
4
Print the DAG task order
Write a print statement that outputs the list of task IDs in the DAG in the order they will run. Use print([task.task_id for task in simple_dag.topological_sort()]).
Apache Airflow
Hint
Use simple_dag.topological_sort() to get tasks in order and print their task_id.
Practice
(1/5)
1. What is a DAG in Apache Airflow?
easy
A. A tool to store data in a database
B. A programming language for writing tasks
C. A type of server used to run Airflow
D. A Directed Acyclic Graph that defines task order without loops
Solution
Step 1: Understand the meaning of DAG
DAG stands for Directed Acyclic Graph, which means tasks are connected in one direction without cycles or loops.
Step 2: Relate DAG to Airflow
In Airflow, DAGs organize tasks so they run in a specific order safely and predictably.
Final Answer:
A Directed Acyclic Graph that defines task order without loops -> Option D
Quick Check:
DAG = Directed Acyclic Graph [OK]
Hint: Remember DAG means tasks flow one way, no loops [OK]
Common Mistakes:
Thinking DAG is a database or server
Confusing DAG with a programming language
Assuming tasks can loop back in DAG
2. Which of the following is the correct way to define a DAG in Airflow using Python?
easy
A. dag = DAG('my_dag', schedule_interval='@daily')
B. dag = DAG('my_dag', run_every='daily')
C. dag = DAG('my_dag', interval='daily')
D. dag = DAG('my_dag', schedule='daily')
Solution
Step 1: Recall Airflow DAG parameters
The correct parameter to set the schedule is schedule_interval.
Step 2: Match the correct syntax
Only dag = DAG('my_dag', schedule_interval='@daily') uses schedule_interval='@daily', which is valid syntax.
Final Answer:
dag = DAG('my_dag', schedule_interval='@daily') -> Option A
Quick Check:
Use schedule_interval to set DAG schedule [OK]
Hint: Use schedule_interval, not run_every or schedule [OK]
Common Mistakes:
Using wrong parameter names like run_every
Forgetting quotes around schedule value
Using invalid schedule strings
3. Given this DAG snippet, what is the order of task execution?
task1 >> task2 >> task3
medium
A. task3 runs first, then task2, then task1
B. All tasks run at the same time
C. task1 runs first, then task2, then task3
D. task2 runs first, then task1, then task3
Solution
Step 1: Understand the '>>' operator in Airflow
The '>>' operator means 'set downstream', so task1 runs before task2, and task2 before task3.
Step 2: Determine execution order
Tasks run in the order task1, then task2, then task3.
Final Answer:
task1 runs first, then task2, then task3 -> Option C
Quick Check:
task1 >> task2 >> task3 means sequential order [OK]
Hint: Remember '>>' means run left task before right task [OK]
Common Mistakes:
Thinking '>>' means run tasks in parallel
Reversing the order of tasks
Assuming tasks run simultaneously
4. What is wrong with this DAG task dependency code?