Data Engineering/Airflow
                
              [Airflow] Task Decorator 간단 사용법
                홍문관카페나무
                 2023. 11. 16. 14:40
              
              
            
            from airflow import DAG
import pendulum
from airflow.decorators import task
with DAG(
    dag_id="example_python_operator",
    schedule="0 9 * * *",
    start_date=pendulum.datetime(2023, 11, 1, tz="Asia/Seoul"),
    catchup=False,
    tags=["example"],
) as dag:
    # [START howto_operator_python]
    @task(task_id="python_task_1")
    def print_context(some_input):
        print(some_input)
    python_task_1 = print_context("task decorator 실행")
airflow의 example_python_operator DAG에 예시가 더 자세히 나와있다.
@task를 붙여주고 파이썬 함수를 담고 있는 변수 명을 task_id로 지정해주면 끝
    # [START howto_operator_python]
    @task(task_id="print_the_context")
    def print_context(ds=None, **kwargs):
        """Print the Airflow context and ds variable from the context."""
        pprint(kwargs)
        print(ds)
        return "Whatever you return gets printed in the logs"
    run_this = print_context()
이런 식으로 파이썬 함수를 담고있는 변수와, task_id를 다르게 지정할 수는 있겠으나
혼동이 생길 수 있으므로, 둘을 같게 지정하는 것도 하나의 팁.
task 데코레이터 사용의 장점으로는 코드가 좀 더 간결해진다는 것이다.
반응형