LoslegenKostenlos starten

Reading a variable in Python

You've realized that using Variables within your Dags will allow a bit of control over the configuration in various environments, including your development and test Airflow servers. To keep from needing to update your Dag code for each, you decide to implement an Output_Path variable to store the location on your server where the various files are kept.

The dag and task objects have been imported for you, along with datetime.

Diese Übung ist Teil des Kurses

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

Übungsanweisungen

  • Import the library necessary to work with Variables.
  • Read the Output_Path variable from Airflow.
  • Provide a fallback value of /home if Output_Path is not defined.

Interaktive praktische Übung

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

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