📖 Scenario: You are setting up a simple Airflow DAG to automate a daily data backup task. To keep your code clean and avoid repetition, you want to use default_args to set common parameters for your DAG and tasks.
🎯 Goal: Create an Airflow DAG named daily_backup that runs daily at 2 AM. Use default_args to set the owner to data_engineer and the start date to January 1, 2024. Then, define a simple task that prints 'Backing up data...'.
📋 What You'll Learn
Create a dictionary called default_args with keys 'owner' and 'start_date' with exact values 'data_engineer' and datetime(2024, 1, 1) respectively.
Create a DAG named daily_backup with default_args=default_args and schedule_interval='0 2 * * *'.
Define a PythonOperator task named backup_task that runs a Python function printing 'Backing up data...'.
Print the DAG's dag_id to confirm setup.
💡 Why This Matters
🌍 Real World
Using <code>default_args</code> helps keep Airflow DAGs clean and consistent, especially when many tasks share the same settings like owner, retries, or start date.
💼 Career
Understanding DAG parameters and default arguments is essential for data engineers and DevOps professionals managing automated workflows with Airflow.
Progress0 / 4 steps
1
Create default_args dictionary
Create a dictionary called default_args with these exact entries: 'owner': 'data_engineer' and 'start_date': datetime(2024, 1, 1). Import datetime from the datetime module.
Apache Airflow
Hint
Use default_args = {'owner': 'data_engineer', 'start_date': datetime(2024, 1, 1)}.
2
Create the DAG with default_args
Import DAG from airflow. Create a DAG named daily_backup using default_args=default_args and schedule_interval='0 2 * * *'.
Apache Airflow
Hint
Use daily_backup = DAG('daily_backup', default_args=default_args, schedule_interval='0 2 * * *').
3
Define the backup task
Import PythonOperator from airflow.operators.python. Define a Python function called backup_function that prints 'Backing up data...'. Then create a PythonOperator task named backup_task using task_id='backup_task', python_callable=backup_function, and dag=daily_backup.
Apache Airflow
Hint
Define a function that prints the message, then create a PythonOperator with the given parameters.
4
Print the DAG ID
Write a print statement to display the dag_id of the DAG daily_backup.
Apache Airflow
Hint
Use print(daily_backup.dag_id) to show the DAG ID.
Practice
(1/5)
1. What is the main purpose of default_args in an Airflow DAG?
easy
A. To define the schedule interval of the DAG
B. To specify the Python version used by the DAG
C. To set common parameters like retries and owner for all tasks
D. To list all the tasks in the DAG
Solution
Step 1: Understand the role of default_args
default_args is a dictionary used to set common settings for tasks, such as retries, owner, start date, etc.
Step 2: Differentiate from DAG parameters
DAG parameters like schedule_interval control when the DAG runs, not task defaults.
Final Answer:
To set common parameters like retries and owner for all tasks -> Option C
Quick Check:
default_args = common task settings [OK]
Hint: default_args sets task defaults, not DAG schedule [OK]
Common Mistakes:
Confusing default_args with DAG schedule_interval
Thinking default_args lists tasks
Assuming default_args sets Python version
2. Which of the following is the correct way to define default_args with retries set to 3 and owner as 'airflow'?
easy
A. default_args = {'retries': 3, 'owner': 'airflow'}
B. default_args = ['retries=3', 'owner=airflow']
C. default_args = (retries=3, owner='airflow')
D. default_args = retries:3, owner:'airflow'
Solution
Step 1: Recall Python dictionary syntax
Python dictionaries use curly braces with key:value pairs, keys as strings and values properly quoted if strings.
Step 2: Check each option's syntax
default_args = {'retries': 3, 'owner': 'airflow'} uses correct dictionary syntax. Options A, B, and D use invalid Python syntax for dictionaries.
Final Answer:
default_args = {'retries': 3, 'owner': 'airflow'} -> Option A
Quick Check:
Python dict = curly braces with key:value [OK]
Hint: default_args is a Python dict with keys and values [OK]
But Airflow throws an error about start_date. What is the likely problem?
medium
A. owner cannot be 'user', must be 'airflow'
B. schedule_interval '@hourly' is invalid syntax
C. default_args must be passed as a list, not dict
D. start_date must be a datetime object, not a string
Solution
Step 1: Check start_date type requirement
Airflow requires start_date to be a datetime object, not a string.
Step 2: Validate other parameters
schedule_interval='@hourly' is valid, default_args is a dict, and owner can be any string.
Final Answer:
start_date must be a datetime object, not a string -> Option D
Quick Check:
start_date = datetime, not string [OK]
Hint: Use datetime objects for start_date in default_args [OK]
Common Mistakes:
Passing start_date as string instead of datetime
Misunderstanding schedule_interval format
Thinking owner must be 'airflow'
5. You want a DAG to run every Monday at 9 AM starting from March 1, 2024, with tasks retrying twice on failure and owned by 'data_team'. Which default_args and DAG parameters are correct?