LoslegenKostenlos starten

Einen Task dekorieren

du hast dich entschieden, einen bestimmten Airflow-Task mit Python zu implementieren. Dieser Task soll mit der Funktion pull_file eine Datei in Airflow herunterladen und im System speichern. Diese Funktion ist bereits für dich definiert.

Die Bibliothek requests sowie das Airflow-Dag-Objekt sind bereits importiert.

Diese Übung ist Teil des Kurses

<Kurs>Einführung in Apache Airflow mit Python</Kurs>
Kurs ansehen

Übungsanweisungen

  • Importiere das benötigte Objekt aus der Airflow-Bibliothek.
  • Konfiguriere die Funktion pull_file als Airflow-Task.
  • Rufe das Task-Objekt auf.

Interaktive praktische Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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()
Code bearbeiten und ausführen