溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spark的基礎(chǔ)介紹和操作調(diào)優(yōu)

發(fā)布時(shí)間:2021-09-14 01:23:54 來(lái)源:億速云 閱讀:115 作者:chen 欄目:云計(jì)算

本篇內(nèi)容介紹了“Spark的基礎(chǔ)介紹和操作調(diào)優(yōu)”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

Spark 基礎(chǔ)介紹

在討論spark調(diào)優(yōu)之前,先看看spark里的一些概念。

action

Action是得到非RDD結(jié)果的RDD操作。如Spark中有如下常見(jiàn)action操作: reduce, collect, count, first, take, takeSample, countByKey, saveAsTextFile

job

每個(gè)spark的action會(huì)被分解成一個(gè)job。

stage

一個(gè)job會(huì)被分成多組task,每組task稱(chēng)為一個(gè)stage。stage的劃分界限為以下兩種task之一:

  • shuffleMapTask - 所有的wide transformation之前,可以簡(jiǎn)單認(rèn)為是shuffle之前

  • resultTask - 可以簡(jiǎn)單認(rèn)為是take()之類(lèi)的操作

partition

RDD 包含固定數(shù)目的 partition, 每個(gè) partiton 包含若干的 record。

narrow tansformation (比如 map 和 filter)返回的 RDD,一個(gè) partition 中的 record 只需要從父 RDD 對(duì)應(yīng)的 partition 中的 record 計(jì)算得到。同樣narrow transformation不會(huì)改變partition的個(gè)數(shù)。

task

被送到executor上執(zhí)行的工作單元; 一個(gè)task只能做一個(gè)stage中的一個(gè)partition的數(shù)據(jù)。 Spark的基礎(chǔ)介紹和操作調(diào)優(yōu)

操作調(diào)優(yōu)

  • 調(diào)整在 stage 邊屆時(shí)的 partition 個(gè)數(shù)經(jīng)常可以很大程度上影響程序的執(zhí)行效率;

  • associative reductive operation, 能使用reduceByKey時(shí)不使用groupByKey,因?yàn)間rouByKey會(huì)把所有數(shù)據(jù)shuffle一遍,而reduceByKey只會(huì)Shuffle reduce的結(jié)果。

  • 輸入和輸出結(jié)果不一樣時(shí),不使用reduceByKey,而使用aggregateByKey;

aggregateByKey: Aggregate the values of each key, using given combine functions and a neutral "zero value". This function can return a different result type, U, than the type of the values in this RDD, V. Thus, we need one operation for merging a V into a U and one operation for merging two U's, as in scala.TraversableOnce. The former operation is used for merging values within a partition, and the latter is used for merging values between partitions. To avoid memory allocation, both of these functions are allowed to modify and return their first argument instead of creating a new U.

  • 不要用flatMap-join-groupBy的模式,可以用cogroup;

  • 當(dāng)兩個(gè)reduceByKey的結(jié)果join時(shí),如果大家的partition都一樣,則spark不會(huì)在join時(shí)做shuffle;

  • 當(dāng)一個(gè)內(nèi)存能放得下的數(shù)據(jù)集join時(shí),可以考慮broadcast而不使用join;

scala> val broadcastVar = sc.broadcast(Array(1, 2, 3))
broadcastVar: org.apache.spark.broadcast.Broadcast[Array[Int]] = Broadcast(0)

scala> broadcastVar.value
res0: Array[Int] = Array(1, 2, 3)

資源調(diào)優(yōu)

spark中的資源可以簡(jiǎn)單歸結(jié)為CPU和內(nèi)存,而以下的參數(shù)會(huì)影響內(nèi)存和CPU的使用。

  • executor 越大并行性越好,越大每個(gè)executor所有的內(nèi)存就越小;

  • core,越大并行性越好;

HDFS client 在大量并發(fā)線程是時(shí)性能問(wèn)題。大概的估計(jì)是每個(gè) executor 中最多5個(gè)并行的 task 就可以占滿的寫(xiě)入帶寬。

  • partition,如果比excutor*core小則很傻;越多每個(gè)partition占用的內(nèi)存就越少;足夠大以后對(duì)性能提升不再有用。

我naive的認(rèn)為應(yīng)該這樣調(diào)整:

  1. core = min(5,cpu核數(shù));

  2. executor = instance數(shù) * cpu核數(shù) / core

  3. 平均每instance的executor個(gè)數(shù)決定executor.memory,從而決定shuffle.memory和storage.memory; Spark的基礎(chǔ)介紹和操作調(diào)優(yōu)

  4. 估計(jì)總數(shù)據(jù)量,即最大的shuffle時(shí)的數(shù)據(jù)大?。╯park driver運(yùn)行記錄中會(huì)有shuffle size);

  5. 用4的結(jié)果除以3得到partition數(shù),如果很小,把partition設(shè)成和(executor*core)的若干倍.

“Spark的基礎(chǔ)介紹和操作調(diào)優(yōu)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI