Operators Basics in Apache Airflow
In the previous chapter, you created your first DAG β a blueprint of execution.
But a blueprint alone doesnβt do anything.
Now itβs time to answer the most important question:
Who actually does the work inside a DAG?
The answer is simple and powerful:
Operatorsβ
What is an Operator in Airflow?β
An Operator defines a single unit of work in Airflow.
Think of operators as workers on an assembly line:
- One runs a Python function
- One executes a shell command
- One simply marks a step as complete
π Key Rule:
One operator = one task
Real-World Story: Operators as Team Membersβ
Imagine a data pipeline team:
| Role | Airflow Operator |
|---|---|
| Engineer running Python logic | PythonOperator |
| System admin running scripts | BashOperator |
| Project manager checkpoint | EmptyOperator |
Each has a clear responsibility.
Thatβs how clean Airflow pipelines are built.
Core Operators You Must Know (Beginner Essentials)β
In this chapter, weβll focus on the three foundational operators:
- PythonOperator
- BashOperator
- EmptyOperator
These alone can build 80% of real-world DAGs.
1οΈβ£ PythonOperatorβ
What is PythonOperator?β
PythonOperator executes a Python callable.
Use it when:
- Processing data
- Calling APIs
- Transforming files
- Writing business logic
Example: Simple Python Taskβ
from airflow.operators.python import PythonOperator
def greet_user():
print("Hello from Airflow!")
Adding PythonOperator to a DAGβ
python_task = PythonOperator(
task_id="greet_task",
python_callable=greet_user
)
Input & Output Exampleβ
Inputβ
No input parameters
Outputβ
Hello from Airflow!
π Output appears in task logs inside the Airflow UI.
Passing Data to PythonOperatorβ
def greet(name):
print(f"Hello {name}")
python_task = PythonOperator(
task_id="personal_greeting",
python_callable=greet,
op_args=["Airflow User"]
)
Outputβ
Hello Airflow User
2οΈβ£ BashOperatorβ
What is BashOperator?β
BashOperator executes shell commands.
Use it when:
- Running scripts
- Calling CLI tools
- Managing files
- Triggering jobs
Example: Bash Taskβ
from airflow.operators.bash import BashOperator
bash_task = BashOperator(
task_id="print_date",
bash_command="date"
)
Input & Output Exampleβ
Inputβ
System shell command
Outputβ
Mon Dec 16 00:00:00 UTC 2024
Real-World BashOperator Exampleβ
bash_task = BashOperator(
task_id="create_directory",
bash_command="mkdir -p /tmp/airflow_demo"
)
π Great for lightweight system-level tasks.
3οΈβ£ EmptyOperatorβ
What is EmptyOperator?β
EmptyOperator does nothing β and thatβs its superpower π¦ΈββοΈ
Use it for:
- Start/end markers
- Logical checkpoints
- DAG readability
Exampleβ
from airflow.operators.empty import EmptyOperator
start = EmptyOperator(task_id="start")
end = EmptyOperator(task_id="end")
Putting It All Together (Complete DAG Example)β
from airflow import DAG
from airflow.operators.empty import EmptyOperator
from airflow.operators.python import PythonOperator
from airflow.operators.bash import BashOperator
from datetime import datetime
def greet():
print("Welcome to Airflow Operators")
with DAG(
dag_id="operators_basics_dag",
start_date=datetime(2024, 1, 1),
schedule_interval="@daily",
catchup=False,
tags=["operators", "airflow"],
) as dag:
start = EmptyOperator(task_id="start")
python_task = PythonOperator(
task_id="python_task",
python_callable=greet
)
bash_task = BashOperator(
task_id="bash_task",
bash_command="echo 'Running Bash Task'"
)
end = EmptyOperator(task_id="end")
start >> python_task >> bash_task >> end
DAG Input & Output Flowβ
Inputβ
- DAG triggered manually or scheduled
- No external data
Outputβ
- start β SUCCESS
- Python log:
Welcome to Airflow Operators
- Bash log:
Running Bash Task
- end β SUCCESS
Best Practices (Professional Grade)β
β
One operator = one responsibility
β
Prefer PythonOperator for logic
β
Keep BashOperator commands simple
β
Use EmptyOperator for clarity
β
Name tasks clearly and consistently
Common Beginner Mistakesβ
β Writing heavy logic inside BashOperator
β Forgetting task_id uniqueness
β Mixing business logic in DAG files
β Overusing Bash when Python is better
Key Takeawaysβ
- Operators define work in Airflow
- PythonOperator runs Python code
- BashOperator runs shell commands
- EmptyOperator improves readability
- Clean operators = maintainable pipelines
Summaryβ
In this chapter, you learned:
- What operators are in Airflow
- How PythonOperator works
- When to use BashOperator
- Why EmptyOperator is important
- How tasks execute inside a DAG
π― You now know how Airflow actually gets work done.
Whatβs Next?β
π Task Dependencies
Learn how to control execution order using:
- set_upstream
- set_downstream
>> and <<