試圖更深入地了解 spark 的作業原理,并嘗試使用 pyspark cli (2.4.0)。我一直在尋找 usinglimit(n).show()和之間的區別show(n)。對于兩個非常相似的查詢,我最終得到了兩個截然不同的性能時間。下面是我運行的命令。下面代碼中參考的鑲木地板檔案大約有 50 列,在遠程 HDFS 上的大小超過 50GB。
# Create dataframe
>>> df = sqlContext.read.parquet('hdfs://hdfs.host/path/to.parquet') ?
# Create test1 dataframe
>>> test1 = df.select('test_col') ?
>>> test1.schema ?
StructType(List(StructField(test_col,ArrayType(LongType,true),true)))
>>> test1.explain() ?
== Physical Plan ==
*(1) Project [test_col#40]
- *(1) FileScan parquet [test_col#40]
Batched: false,
Format: Parquet,
Location: InMemoryFileIndex[hdfs://hdfs.host/path/to.parquet],
PartitionCount: 25,
PartitionFilters: [],
PushedFilters: [],
ReadSchema: struct<test_col:array<bigint>>
# Create test2 dataframe
>>> test2 = df.select('test_col').limit(5) ?
>>> test2.schema ?
StructType(List(StructField(test_col,ArrayType(LongType,true),true)))
>>> test2.explain() ?
== Physical Plan ==
CollectLimit 5
- *(1) Project [test_col#40]
- *(1) FileScan parquet [test_col#40]
Batched: false,
Format: Parquet,
Location: InMemoryFileIndex[hdfs://hdfs.host/path/to.parquet],
PartitionCount: 25,
PartitionFilters: [],
PushedFilters: [],
ReadSchema: struct<test_col:array<bigint>>
請注意,實際規劃是兩個幾乎相同的test1和test2。唯一的例外是 test2 的計劃以“CollectLimit 5”開頭。設定好之后,我跑了test1.show(5)然后test2.show(5)。測驗 1 立即回傳結果。測驗 2 顯示了一個帶有 2010 任務的進度條,大約需要 20 分鐘才能完成(我只有一個 executor)
問題 為什么測驗 2(有限制)與測驗 1(無限制)相比表現如此差?資料集和結果集相同,物理計劃幾乎相同。
uj5u.com熱心網友回復:
記住:
show()是別名show(20)并依賴于內部take(n: Int): Array[T]limit(n: Int)回傳另一個資料集并且是讀取整個源的昂貴操作
uj5u.com熱心網友回復:
限制 - 導致新資料幀并花費更長的時間,因為這是因為您的輸入檔案格式當前不支持謂詞下推。因此讀取整個資料集并應用限制。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/365213.html
