새소식

[5분 내로] 강의실/📋 Airflow 도입하기

🏅5분 내로 airflow의 dag 만들어서 실행하기

  • -
728x90

🔥 TODO

👉 5분 내로 airflow의 dag 만들어서 실행해봅시다!

🧙 Solution

👉 작업 요약 : 파이썬 코드를 생성한 후 DAG가 모여있는 폴더에 넣으시면 됩니다.

1. hello_world_brother.py 생성

import pendulum
from datetime import datetime
from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.python_operator import PythonOperator

# timezone 한국시간으로 변경
kst = pendulum.timezone("Asia/Seoul")

# 기본 args 생성
default_args = {
    'owner' : 'Hello World',
    'email' : ['airflow@airflow.com'],
    'email_on_failure': False,
}

# DAG 생성 
# 2022/06/21 @once 한번만 실행하는 DAG생성
with DAG(
    dag_id="hello_world_brother",
    default_args=default_args,
    start_date=datetime(2022, 6,21, tzinfo=kst),
    description='print hello world',
    schedule_interval='@once',
    tags=['test']
) as dag:

    # python Operator에서 사용할 함수 정의
    def print_hello():
        print('hello world')

    t1 = DummyOperator(
        task_id='dummy_task_id',
        retries=5,
    )

    t2 = PythonOperator(
        task_id='Hello_World',
        python_callable=print_hello
    )

    t1 >> t2

 

2. 위에서 제작한 .py를 DAG 경로에 붙여넣기

그냥 DAG가 모여있는 리눅스 경로에 .py를 붙여 넣으면, Airflow가 알아서 인식합니다.

※ Airflow WEB UI에서 뭔가 버튼을 눌러서 추가하고 저장하는.. 그런 것이 아닙니다.

 

2.1 참고) 저의 경우 아래의 경로가 DAG입니다.

/usr/local/lib/python3.10/dist-packages/airflow/example_dags/

※ DAG 경로는 당연히.. 사용자 환경에 따라 달라질 수 있습니다.

 

2.2 참고) dag 경로가 어딘지 모를 경우

find ~ -name "*dag*"

위 명령어로 찾으시면 됩니다.

디렉터리 포함 전체 검색 명령어입니다.

 

2.3 참고) 혹시 권한이 없는 경우

리눅스를 사용하시는 경우, DAG 폴더 접근 권한이 없을 수도 있습니다.

아래의 명령어 필요하시죠? 적절히 권한 조정하셔서 쓰시면 됩니다.

chmod 777 example_dags/

 

🏅 Result

👉 Airflow Web UI에서 작업 결과를 점검하겠습니다.

만드신 DAG에 오타 등으로 인한 에러가 없다면, Airflow가 알아서 인식할 것입니다.

1. Airflow Web UI 접속

URL : http://localhost:8080/home

 

2. 우측 상단에 존재하는 새로고침 버튼을 눌러줍니다.

 

3. 30초 정도 대기 후.. 위에서 제작한 DAG가 Airflow에 등록됬는지 체크하시면 됩니다.

 

If I was of any help to you, please buy me coffee 😿😢😥

If you have any questions, please leave them in the comments

🧭 References

[1] reference : https://doctorson0309.tistory.com/

[2] Ads : https://play.google.com/store/apps/details?id=io.cordova.seoulfilter

반응형
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.