Airflow Study8- Task Decorator

박성현·2024년 5월 29일

Airflow

목록 보기
14/28

Python은 함수 안에 함수를 선언 가능 .

AIRFLOW 마스터 클래스 내 자료 활용

AS - IS :


모든 get_data()를 함수를 outer_func로 감싸서 수정해야 함. 불편함

TO - BE :


dags_python_task_decorator.py



from airflow.models.dag import DAG

import pendulum
from airflow.decorators import task



with DAG(
    dag_id="dags_python_task_decorator",
    schedule="0 2 * * 1",
    start_date=pendulum.datetime(2021, 1, 1, tz="Asia/Seoul"),
    catchup=False,
    
) as dag:
    
    @task(task_id="python_task_1")
    def print_context(some_input):
        print(some_input)
        
    python_task_1 = print_context('task_decorator 실행')

추가 task decorator 연결 관계 시 task1() >> task2() 같이 함수호출로 연결관계 표시 가능
+ task Decorator 뿐만아니라, dag Decorator도 있는데 이는 나중에 추가 작성 아래 예시 참고

@dag(start_date=datetime(2023, 1, 1), schedule_interval=None)
def my_dag():

    @task
    def task_1():
        return "Hello from task 1!"

    @task
    def task_2(message):
        print(message)

    @task
    def task_3():
        print("Hello from task 3!")

    task_2(task_1()) >> task_3()  # task_1 -> task_2 -> task_3 순서로 실행

참고 자료 : AIRFLOW 마스터 클래스 내 자료 활용

profile
다소Good한 데이터 엔지니어

0개의 댓글