Bird
Raised Fist0
Apache Airflowdevops~5 mins

DAG concept (Directed Acyclic Graph) in Apache Airflow - Commands & Configuration

Choose your learning style10 modes available
Introduction
A DAG in Airflow is a way to organize tasks so they run in order without looping back. It helps you automate workflows by defining which tasks run first and which run after.
When you want to run a series of data processing steps one after another automatically.
When you need to schedule tasks to run at specific times without manual intervention.
When you want to make sure tasks don’t run in a circle causing endless loops.
When you want to track the progress and success of each step in a workflow.
When you want to retry failed tasks without restarting the whole process.
Config File - example_dag.py
example_dag.py
from airflow import DAG
from airflow.operators.bash import BashOperator
from datetime import datetime, timedelta

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': datetime(2024, 6, 1),
    'retries': 1,
    'retry_delay': timedelta(minutes=5),
}

dag = DAG(
    'example_dag',
    default_args=default_args,
    description='A simple DAG example',
    schedule_interval='@daily',
    catchup=False,
)

task1 = BashOperator(
    task_id='print_date',
    bash_command='date',
    dag=dag,
)

task2 = BashOperator(
    task_id='sleep',
    bash_command='sleep 5',
    dag=dag,
)

task3 = BashOperator(
    task_id='echo_hello',
    bash_command='echo Hello Airflow',
    dag=dag,
)

# Define task order
 task1 >> task2 >> task3

This file defines a DAG named 'example_dag' that runs daily starting June 1, 2024.

It has three tasks: print_date, sleep, and echo_hello.

The tasks run in order: print_date first, then sleep, then echo_hello.

Default arguments set retry behavior and owner info.

Commands
This command lists all DAGs currently available in Airflow to confirm your DAG is recognized.
Terminal
airflow dags list
Expected OutputExpected
example_dag
This command manually starts a run of the example_dag to test if tasks execute in order.
Terminal
airflow dags trigger example_dag
Expected OutputExpected
Created <DagRun example_dag @ 2024-06-01T00:00:00+00:00: manual__2024-06-01T00:00:00+00:00, externally triggered: True>
This command lists all tasks in the example_dag to verify the tasks defined in the DAG file.
Terminal
airflow tasks list example_dag
Expected OutputExpected
print_date sleep echo_hello
This command runs the print_date task for the given date without affecting the DAG run state, useful for debugging.
Terminal
airflow tasks test example_dag print_date 2024-06-01
Expected OutputExpected
[2024-06-01 00:00:00,000] {bash.py:123} INFO - Running command: date [2024-06-01 00:00:00,100] {bash.py:130} INFO - Output: Wed Jun 1 00:00:00 UTC 2024 [2024-06-01 00:00:00,200] {taskinstance.py:123} INFO - Task succeeded
Key Concept

If you remember nothing else from this pattern, remember: a DAG is a set of tasks arranged so they run in order without loops.

Common Mistakes
Creating tasks without setting dependencies between them
Tasks will run in any order or all at once, breaking the intended workflow sequence.
Use >> or set_upstream/set_downstream to define the order tasks should run.
Defining a cycle in task dependencies (e.g., task1 >> task2 >> task1)
Airflow will fail to run the DAG because cycles cause infinite loops and break the acyclic rule.
Ensure dependencies form a directed acyclic graph with no loops.
Not setting a start_date or setting it in the future
The DAG will not run because Airflow schedules tasks starting from the start_date.
Set start_date to a past or current date to enable scheduling.
Summary
Define a DAG file with tasks and set their order using dependencies.
Use airflow CLI commands to list DAGs, trigger runs, and test tasks.
Remember a DAG must be acyclic and tasks run in the order you set.

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

  1. 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.
  2. Step 2: Relate DAG to Airflow

    In Airflow, DAGs organize tasks so they run in a specific order safely and predictably.
  3. Final Answer:

    A Directed Acyclic Graph that defines task order without loops -> Option D
  4. 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

  1. Step 1: Recall Airflow DAG parameters

    The correct parameter to set the schedule is schedule_interval.
  2. Step 2: Match the correct syntax

    Only dag = DAG('my_dag', schedule_interval='@daily') uses schedule_interval='@daily', which is valid syntax.
  3. Final Answer:

    dag = DAG('my_dag', schedule_interval='@daily') -> Option A
  4. 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

  1. Step 1: Understand the '>>' operator in Airflow

    The '>>' operator means 'set downstream', so task1 runs before task2, and task2 before task3.
  2. Step 2: Determine execution order

    Tasks run in the order task1, then task2, then task3.
  3. Final Answer:

    task1 runs first, then task2, then task3 -> Option C
  4. 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?
task1.set_downstream(task2)
task2.set_downstream(task1)
medium
A. It creates a cycle, which is not allowed in DAGs
B. The syntax for set_downstream is incorrect
C. Tasks must be named differently
D. No error, this is valid

Solution

  1. Step 1: Analyze the dependencies

    task1 depends on task2, and task2 depends on task1, creating a loop.
  2. Step 2: Understand DAG rules

    DAGs cannot have cycles; tasks must not depend on each other circularly.
  3. Final Answer:

    It creates a cycle, which is not allowed in DAGs -> Option A
  4. Quick Check:

    DAGs must be acyclic, no loops allowed [OK]
Hint: Check for circular dependencies to avoid cycles [OK]
Common Mistakes:
  • Ignoring circular dependencies
  • Assuming set_downstream syntax is wrong
  • Thinking task names cause errors
5. You want to create a DAG where task3 runs only after both task1 and task2 complete. Which code correctly sets this dependency?
hard
A. task3 >> task1 task3 >> task2
B. task1 >> task3 task2 >> task3
C. task1 << task3 task2 << task3
D. task3.set_downstream([task1, task2])

Solution

  1. Step 1: Understand dependency direction

    To run task3 after task1 and task2, task1 and task2 must be upstream of task3.
  2. Step 2: Check correct syntax

    Using '>>' from task1 and task2 to task3 sets task3 to run after both complete.
  3. Final Answer:

    task1 >> task3 task2 >> task3 -> Option B
  4. Quick Check:

    Upstream tasks >> downstream task [OK]
Hint: Use 'task1 >> task3' and 'task2 >> task3' for multiple dependencies [OK]
Common Mistakes:
  • Reversing dependency arrows
  • Using set_downstream incorrectly with lists
  • Assuming task3 runs before task1 and task2
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard