0
0
Apache Airflowdevops~10 mins

Creating custom operators in Apache Airflow - Visual Walkthrough

Choose your learning style9 modes available
Process Flow - Creating custom operators
Define Custom Operator Class
Inherit from BaseOperator
Override execute() Method
Use Custom Operator in DAG
Airflow Scheduler Runs DAG
Custom Operator execute() Runs
Task Completes or Fails
This flow shows how to create a custom operator by defining a class, overriding its execute method, then using it in a DAG where Airflow runs it.
Execution Sample
Apache Airflow
from airflow.models import BaseOperator

class MyOperator(BaseOperator):
    def execute(self, context):
        print('Hello from MyOperator')
Defines a custom operator class that prints a message when executed.
Process Table
StepActionEvaluationResult
1Define MyOperator class inheriting BaseOperatorClass createdReady to use in DAG
2Override execute() methodMethod definedPrints message when run
3Instantiate MyOperator in DAGTask createdTask ready for scheduling
4Airflow scheduler triggers taskTask startsexecute() method runs
5execute() prints messageOutput: Hello from MyOperatorTask completes successfully
6Task status updatedSuccess recordedDAG continues or ends
💡 Task completes after execute() method runs and prints message
Status Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
MyOperator classNot definedDefined with execute()Instance createdexecute() runsTask done
execute() outputNoneDefinedNone'Hello from MyOperator'Printed
Key Moments - 2 Insights
Why do we override the execute() method in the custom operator?
Because the execute() method contains the code that runs when the task executes, as shown in execution_table step 4 and 5.
Can we use the custom operator without inheriting BaseOperator?
No, inheriting BaseOperator is required to integrate with Airflow's task system, as shown in execution_table step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 4?
AThe Airflow scheduler triggers the task and execute() runs
BThe custom operator class is defined
CThe task status is updated to success
DThe execute() method is overridden
💡 Hint
Refer to execution_table row with Step 4 describing task start and execute() running
According to variable_tracker, what is the output of execute() after step 5?
ANone
B'Hello from MyOperator'
CTask instance
DClass definition
💡 Hint
Check execute() output column after Step 5 in variable_tracker
If we do not inherit BaseOperator, what will happen according to the key moments?
AThe task will run normally
Bexecute() will run twice
CAirflow will not recognize the custom operator as a task
DThe DAG will fail to parse
💡 Hint
See key_moments about inheritance requirement for Airflow integration
Concept Snapshot
Creating custom operators in Airflow:
- Define a class inheriting BaseOperator
- Override execute(self, context) method
- Put task logic inside execute()
- Use the operator in DAGs like any other task
- Airflow runs execute() when task runs
- Enables reusable, custom task behavior
Full Transcript
To create a custom operator in Airflow, you start by defining a new class that inherits from BaseOperator. This inheritance is necessary so Airflow can treat your class as a task. Next, you override the execute() method in your class. This method contains the code that runs when the task executes. For example, you can print a message inside execute(). After defining your custom operator, you use it in your DAG by creating an instance of it as a task. When the Airflow scheduler runs your DAG, it triggers your custom operator's execute() method. The task runs, prints the message, and completes successfully. This process allows you to create reusable, custom task logic integrated with Airflow's scheduling and execution system.