開始使用免費開始

快速 pipeline

在你開始剖析更複雜的資料之前,主管想先看到一個包含基本步驟的簡單 pipeline 範例。這個範例中,你需要讀入一個資料檔、篩選出幾列、加上一個 ID 欄位,然後把結果寫成 JSON 資料。

spark 內容已經定義好,且 pyspark.sql.functions 函式庫也已依慣例以 F 作為別名。

本練習屬於課程

使用 PySpark 清理資料

檢視課程

練習說明

  • 將檔案 2015-departures.csv.gz 匯入為一個 DataFrame。注意檔頭已經定義好。
  • 將 DataFrame 篩選為只包含飛行時間大於 0 分鐘的航班。請使用欄位的「索引」而非欄位名稱(記得用 .printSchema() 檢視欄位名稱與順序)。
  • 新增一個 ID 欄位。
  • 將結果寫出為名稱為 output.json 的 JSON 文件。

動手互動練習

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

# Import the data to a DataFrame
departures_df = spark.____(____, header=____)

# Remove any duration of 0
departures_df = departures_df.____(____)

# Add an ID column
departures_df = departures_df.____('id', ____)

# Write the file out to JSON format
____.write.____(____, mode='overwrite')
編輯並執行程式碼