開始使用免費開始

撰寫 Spark 設定

既然你已經檢視過叢集上的部分 Spark 設定,接下來要調整一些參數,讓 Spark 更貼近你的需求。你會匯入一些資料,確認變更確實影響到叢集。

Spark 的設定一開始為預設的 200 個分割區(partitions)。

spark 物件可直接使用。名為 departures.txt.gz 的檔案可供匯入。從 departures.txt.gz 取得的唯一列所組成的初始 DataFrame 已以 departures_df 提供。

本練習屬於課程

使用 PySpark 清理資料

檢視課程

練習說明

  • departures_df 的分割區數量存到變數 before
  • spark.sql.shuffle.partitions 設定改為 500 個分割區。
  • 重新建立 departures_df,從 departures 檔案讀取並擷取唯一列。
  • 列印設定變更前與變更後的分割區數量。

動手互動練習

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

# Store the number of partitions in variable
before = departures_df.____

# Configure Spark to use 500 partitions
____('spark.sql.shuffle.partitions', ____)

# Recreate the DataFrame using the departures data file
departures_df = spark.read.csv('departures.txt.gz').____

# Print the number of partitions for each instance
print("Partition count before change: %d" % ____)
print("Partition count after change: %d" % ____)
編輯並執行程式碼