태스크 데코레이팅
Airflow에서 특정 태스크를 Python으로 구현하려고 합니다. 이 태스크는 pull_file 함수를 사용해 Airflow 내에서 파일을 다운로드하고 시스템에 저장해야 합니다. pull_file 함수는 이미 정의되어 있습니다.
requests 라이브러리와 Airflow dag 객체는 이미 임포트되어 있습니다.
이 연습은 강의의 일부입니다
Python으로 배우는 Apache Airflow 입문
연습 안내
- Airflow 라이브러리에서 필요한 객체를 임포트하세요.
pull_file함수가 Airflow 태스크로 동작하도록 설정하세요.- 태스크 객체를 호출하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Import the task decorator
from ____ import ____
@dag(dag_id='file_update')
def file_update():
# Decorate the function as a task
____
def pull_file(URL, savepath):
r = requests.get(URL)
with open(savepath, 'wb') as f:
f.write(r.content)
print(f"File pulled from {URL} and saved to {savepath}")
# Call the task
____('http://dataserver/sales.json', 'latestsales.json')
file_update()