Python에서 변수 읽기
DAG 내에서 Variables를 활용하면 개발 서버와 테스트 Airflow 서버를 포함한 다양한 환경에서 설정을 유연하게 제어할 수 있다는 점을 깨달았습니다. 환경마다 DAG 코드를 수정하지 않아도 되도록, 서버에서 각종 파일이 저장된 위치를 보관하는 Output_Path 변수를 구현하기로 했습니다.
dag와 task 객체는 datetime과 함께 이미 임포트되어 있습니다.
이 연습은 강의의 일부입니다
Python으로 배우는 Apache Airflow 입문
연습 안내
- Variables를 사용하는 데 필요한 라이브러리를 임포트하세요.
- Airflow에서
Output_Path변수를 읽어 오세요. Output_Path가 정의되지 않은 경우를 대비해/home을 대체 값으로 지정하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Import the proper library to read Variables
from airflow.____ import ____
@dag(dag_id='process_sales', start_date=datetime(2026, 4, 15))
def process_sales():
@task()
def parse_file():
# Get the output file location, otherwise default to "/home"
output_path = ____.____("Output_Path", ____="/home")
# Logging only currently - processing to be added later
print(f"File parsed and saved to {output_path}/sales_report.pdf")
parse_file()
process_sales()