開始使用免費開始

更多 @tasks

為了繼續完成你的工作流程,你需要再加入一步,來剖析並儲存已下載檔案的變更。Dag process_sales 已經定義好,並且已加入 pull_file 這個 task。這裡,已經替你定義好對應的 Python 函式 parse_file(inputfile, outputfile)

請注意,在實作 Airflow tasks 時,你不一定會完全理解每個提供給你的步驟。只要你了解如何把這些步驟包進 Airflow 的結構裡,你就能實作出想要的工作流程。

本練習屬於課程

Python 中的 Apache Airflow 入門

檢視課程

練習說明

  • 使用 parse_file 方法建立一個 Airflow task。
  • 以必要的引數呼叫該 task。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

@dag(dag_id='process_sales')
def process_sales():
    # Decorate parse_file as a task
    ____
    def parse_file(inputfile: str, outputfile: str):
        with open(inputfile) as infile:
            data = json.load(infile)
            with open(outputfile, 'w') as outfile:
                json.dump(data, outfile)

    pull_file('http://dataserver/sales.json', 'latestsales.json')
    # Call the parse_file task
    ____('latestsales.json', 'latestsales_parsed.json')

process_sales()
編輯並執行程式碼