Decorating a task
You've decided to implement a specific Airflow task using Python. This task needs to download and save a file to the system within Airflow, using the pull_file function. This function is already defined for you.
The requests library is imported for you, as is the Airflow dag object.
This exercise is part of the course
Introduction to Apache Airflow in Python
Exercise instructions
- Import the necessary object from the Airflow library.
- Configure the
pull_filefunction to be an Airflow task. - Call the task object.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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()