Software Engineering

How to use TaskFlow API (@task) while using an Operator other than Python

머니기어 2024. 2. 2. 12:02
반응형

Created by Bing Image Creator with "data vane" as prompt

Introduction

 TaskFlow API paradigm is introduced as part of Airflow 2.0 and contrasts this with DAGs written using the traditional paradigm. In this data pipeline, tasks are created based on Python functions simply using the @task decorator. The function name acts as a unique identifier for the task. But how do I use Python and other operators together? Let's check it out.

 

QuickStart

You just simply add context as a parameter of python function and throw this to some operator inside of it. Below is an example.

@dag()
def dag():

    @task()
    def python_task(**context):
    	bash_task = BashOperator(
            task_id='bash_task',
            bash_command='echo "this is BashOperator"',
        )
        bash_task.execute(context)

    task()

 

 Any Operator is executed internally wiht execute(). So, you can manually write the code in @task decorated python function. But you must offer the context as a payload which contains information of task instance. In this case, "task_id" parameter is required for creating instance of BashOperator, but it is overwritten as "python_task".

반응형