spark broadcoast 廣播變量

https://blog.csdn.net/weixin_41804049/article/details/79903472

個人理解: spark driver 會將普通變量發送到每個task中,如果該變量特別大,會導致內存溢出,所以,使用廣播變量,driver將變量廣播到每個executor中,每個task向executor去取需要的變量,即可避免內存溢出。(這個理解並不全面,廣播變量 在多個stages 中都需要相同的數據時,就把這個數據定義成廣播變量。)

官網中解釋:

Broadcast variables allow the programmer to keep a read-only variable cached on each machine rather than shipping a copy of it with tasks. They can be used, for example, to give every node a copy of a large input dataset in an efficient manner. Spark also attempts to distribute broadcast variables using efficient broadcast algorithms to reduce communication cost.

Spark actions are executed through a set of stages, separated by distributed “shuffle” operations. Spark automatically broadcasts the common data needed by tasks within each stage. The data broadcasted this way is cached in serialized form and deserialized before running each task. This means that explicitly creating broadcast variables is only useful when tasks across multiple stages need the same data or when caching the data in deserialized form is important.

(1)當任務跨越了多個stages,並且需要相同的數據;     (stages的劃分是由 shufflue決定的,shufflue是由寬依賴的RDD產生,寬依賴:子分區的數據是由有多個父分區數據處理得來;窄依賴:父分區的數據只會流向一個子分區 )

(2)當用反序列化的形式緩存數據的時候;

廣播變量:

比如數據庫中一份公共配置表格,需要同步給各個節點進行查詢。

廣播變量允許程序緩存一個只讀的變量在每臺機器上面,而不是每個任務保存一份拷

貝。例如,利用廣播變量,我們能夠以一種更有效率的方式將一個大數據量輸入集合的

副本分配給每個節點。Spark也嘗試着利用有效的廣播算法去分配廣播變量,以減少通

信的成本。
一個廣播變量可以通過調用SparkContext.broadcast(v)方法從一個初始變量v中創建。廣

播變量是v的一個包裝變量,它的值可以通過value方法訪問,下面的代碼說明了這個過

程:

WordCount程序 詳細圖解可參照 連接https://blog.csdn.net/weixin_41804049/article/details/79903472

廣播變量圖解:

廣播變量之hive優化

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章