📖 Scenario: You are working with Apache Airflow to automate data workflows. Instead of writing complex code for each task, Airflow uses operators to simplify and reuse common tasks like running a bash command or executing a Python function.
🎯 Goal: Build a simple Airflow DAG that uses operators to run a bash command and a Python function, showing how operators help abstract common tasks.
📋 What You'll Learn
Create a DAG with a specific dag_id
Use BashOperator to run a bash command
Use PythonOperator to run a Python function
Set task dependencies to run bash task before python task
Print the output of the Python function
💡 Why This Matters
🌍 Real World
In real projects, operators let you quickly build workflows without rewriting code for common tasks like running scripts or sending emails.
💼 Career
Understanding operators is key for DevOps and data engineering roles that use Airflow to automate and manage data pipelines efficiently.
Progress0 / 4 steps
1
Create the initial DAG setup
Import DAG from airflow and datetime from datetime. Create a DAG object called dag with dag_id='example_operator_dag' and start_date=datetime(2024, 1, 1).
Apache Airflow
Hint
Use DAG(dag_id='example_operator_dag', start_date=datetime(2024, 1, 1)) to create the DAG.
2
Add a BashOperator task
Import BashOperator from airflow.operators.bash. Create a task called bash_task using BashOperator with task_id='print_date', bash_command='date', and assign it to the dag.
Apache Airflow
Hint
Use BashOperator(task_id='print_date', bash_command='date', dag=dag) to create the task.
3
Add a PythonOperator task
Import PythonOperator from airflow.operators.python. Define a function called print_hello that prints 'Hello from PythonOperator'. Create a task called python_task using PythonOperator with task_id='say_hello', python_callable=print_hello, and assign it to the dag. Set bash_task to run before python_task.
Apache Airflow
Hint
Define the function first, then create PythonOperator with python_callable=print_hello. Use bash_task >> python_task to set order.
4
Print the PythonOperator output
Add a print statement to display 'DAG with operators created successfully' after the task definitions.
Apache Airflow
Hint
Use print('DAG with operators created successfully') to show the message.
Practice
(1/5)
1. Why do Airflow operators help when building workflows?
easy
A. They let you reuse common tasks easily and keep code simple
B. They make workflows run faster by using more CPU
C. They automatically fix errors in your code
D. They replace the need for writing any Python code
Solution
Step 1: Understand the purpose of operators
Operators are designed to represent common tasks in workflows so you don't rewrite code each time.
Step 2: Recognize benefits of using operators
Using operators keeps your workflow code organized, simpler, and easier to read.
Final Answer:
They let you reuse common tasks easily and keep code simple -> Option A
Quick Check:
Operators simplify workflows = They let you reuse common tasks easily and keep code simple [OK]
Hint: Operators = reusable task blocks in workflows [OK]
Common Mistakes:
Thinking operators speed up execution
Believing operators fix code errors automatically
Assuming operators remove all coding
2. Which of the following is the correct way to use an Airflow operator in a DAG?
easy
A. task = BashOperator('run', 'echo Hello')
B. task = BashOperator(task='run', command='echo Hello')
C. task = BashOperator(bash_command='echo Hello')
D. task = BashOperator(task_id='run', bash_command='echo Hello')
Solution
Step 1: Recall BashOperator syntax
BashOperator requires named parameters: task_id and bash_command.
Step 2: Check each option's syntax
task = BashOperator(task_id='run', bash_command='echo Hello') uses correct parameter names and syntax; others miss required names or use wrong ones.
Final Answer:
task = BashOperator(task_id='run', bash_command='echo Hello') -> Option D
B. Parameter name 'bash_cmd' is incorrect; should be 'bash_command'
C. BashOperator cannot run echo commands
D. Missing task_id parameter
Solution
Step 1: Check parameter names for BashOperator
BashOperator requires 'bash_command' parameter, not 'bash_cmd'.
Step 2: Verify other parameters
task_id is present and valid; echo commands are allowed; 'run' is valid task_id.
Final Answer:
Parameter name 'bash_cmd' is incorrect; should be 'bash_command' -> Option B
Quick Check:
Correct parameter name is bash_command = Parameter name 'bash_cmd' is incorrect; should be 'bash_command' [OK]
Hint: Use exact parameter names: bash_command, not bash_cmd [OK]
Common Mistakes:
Using wrong parameter names
Thinking task_id is missing
Assuming BashOperator can't run echo
5. You want to run a Python function repeatedly in your Airflow DAG. Why is it better to use the PythonOperator instead of writing the function call directly in the DAG file?
hard
A. PythonOperator automatically fixes bugs in your function
B. Direct function calls run faster than PythonOperator
C. PythonOperator abstracts the task, making it reusable and easier to manage
D. Using PythonOperator removes the need to write the function
Solution
Step 1: Understand PythonOperator role
PythonOperator wraps a Python function as a task, allowing reuse and better workflow structure.
Step 2: Compare direct calls vs operator usage
Direct calls run immediately and don't integrate with Airflow's scheduling or retries; operators manage these features.
Final Answer:
PythonOperator abstracts the task, making it reusable and easier to manage -> Option C
Quick Check:
Operators improve task reuse and management = PythonOperator abstracts the task, making it reusable and easier to manage [OK]
Hint: Use PythonOperator to wrap functions as tasks [OK]
Common Mistakes:
Thinking direct calls are better for reuse
Believing operators fix bugs automatically
Assuming operators remove need to write code
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.