0
0
Apache Airflowdevops~20 mins

Creating custom operators in Apache Airflow - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Purpose of Custom Operators in Airflow

Why would you create a custom operator in Apache Airflow instead of using built-in operators?

ATo encapsulate reusable logic specific to your workflows that built-in operators do not provide.
BBecause built-in operators cannot be used in DAGs.
CTo avoid writing any Python code in your DAG files.
DTo automatically speed up task execution without any configuration.
Attempts:
2 left
💡 Hint

Think about when you need to do something unique or repetitive in your workflows.

💻 Command Output
intermediate
2:00remaining
Output of Custom Operator Execution

Given this custom operator code snippet, what will be printed when the task runs?

Apache Airflow
from airflow.models import BaseOperator
from airflow.utils.decorators import apply_defaults

class HelloOperator(BaseOperator):
    @apply_defaults
    def __init__(self, name: str, **kwargs):
        super().__init__(**kwargs)
        self.name = name

    def execute(self, context):
        print(f"Hello, {self.name}!")

# Assume this operator is used in a DAG with name='Airflow'
ANo output printed
BHello, Airflow!
CError: __init__ missing required positional argument
DHello, name!
Attempts:
2 left
💡 Hint

Look at how the name parameter is used in execute.

Configuration
advanced
2:30remaining
Correct Custom Operator Initialization

Which option correctly defines a custom operator that accepts a file_path parameter and passes other arguments properly to the base class?

A
class FileOperator(BaseOperator):
    def __init__(self, file_path, **kwargs):
        super().__init__(**kwargs)
        self.file_path = file_path
B
class FileOperator(BaseOperator):
    def __init__(self, **kwargs):
        self.file_path = kwargs.pop('file_path')
        super().__init__(**kwargs)
C
class FileOperator(BaseOperator):
    def __init__(self, file_path, **kwargs):
        self.file_path = file_path
        super().__init__(**kwargs)
D
class FileOperator(BaseOperator):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.file_path = kwargs['file_path']
Attempts:
2 left
💡 Hint

Remember to call super().__init__ before using self attributes.

Troubleshoot
advanced
2:00remaining
Error Raised by Incorrect Custom Operator Code

What error will this custom operator code raise when instantiated?

Apache Airflow
class BadOperator(BaseOperator):
    def __init__(self, param, **kwargs):
        self.param = param
        super().__init__(**kwargs)
ARuntimeError: Task instance failed
BAttributeError: 'BadOperator' object has no attribute 'param'
CTypeError: __init__() missing 1 required positional argument: 'task_id'
DNo error, runs fine
Attempts:
2 left
💡 Hint

Check the order of super().__init__ call and attribute assignment.

🔀 Workflow
expert
3:00remaining
Order of Steps to Create and Use a Custom Operator

What is the correct order of steps to create and use a custom operator in Airflow?

A3,1,2,4
B2,1,3,4
C1,3,2,4
D1,2,3,4
Attempts:
2 left
💡 Hint

Think about writing code, importing it, then using it in the DAG.