为任务添加装饰器
您打算用 Python 实现一个特定的 Airflow 任务。该任务需要在 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()