[SPARK] RDD, Action 和 Transformation (1)

在之前介紹 SparkPi 程式時, 我們看到兩個特殊的指令: map, reduce.
我們先列出官方指南中的介紹:

  • map (func): Return a new distributed dataset formed by passing each element of the source through a function func.
  • reduce (func): Aggregate the elements of the dataset using a function func (which takes two arguments and returns one). The function should be commutative and associative so that it can be computed correctly in parallel.
簡單來說, map 就是對數據進行運算, 得到一組轉換的結果,
在 SparkPi 中, 所做的對應是藉由 Monte Carlo 方式, 取得 0 或 1的數值,
而 reduce 則是聚集數據集中的所有元素, 定義為: 對兩個參數運算, 回報一個結果,
在 SparkPi 中, 把兩個數值相加, 回報相加結果.

特別的是, 在 Spark 中, map, reduce 分屬不同類別,
map 是 transformations, 而 reduce 是 actions, 定義如下:
  • transformations, which create a new dataset from an existing one
  • actions, which return a value to the driver program after running a computation on the dataset.
在 Spark 中, transformations 本身不會主動被執行,
只有在相對應的 actions 執行時, 才會進行,
這樣的特性和 hadoop 中的 map-reduce 並不相同,
p.s. hadoop 中的 map-reduce 視為兩種不同映射函數, 並無從屬關係,

Spark 架構改變大概是因為多階段平行運算架構的支援,
像是下圖的平行架構:


考慮到不同平行階段不一定同步,
我們需要同步的查核點, 來確保不同 "階段",
此時, actions (也就是範例中的 reduce), 就扮演了這樣的角色,

而更多的 actions 和 transformations, 可以分別參考:
https://spark.apache.org/docs/2.1.0/programming-guide.html#transformations
https://spark.apache.org/docs/2.1.0/programming-guide.html#actions

留言

熱門文章

LTE筆記: RSRP, RSSI and RSRQ

[WiFi] WiFi 網路的識別: BSS, ESS, SSID, ESSID, BSSID

LTE筆記: 波束成型 (beamforming) 和天線陣列