天天看點

spark-submit簡要說明

首先通過bin/spark-submit --help檢視一下spark-submit指令的幫助說明:

spark-submit簡要說明
[[email protected] discover]$ bin/spark-submit --help
Usage: spark-submit [options] <app jar | python file> [app arguments]
Usage: spark-submit --kill [submission ID] --master [spark://...]
Usage: spark-submit --status [submission ID] --master [spark://...]

Options:
  --master MASTER_URL         spark://host:port, mesos://host:port, yarn, or local.
  --deploy-mode DEPLOY_MODE   Whether to launch the driver program locally ("client") or
                              on one of the worker machines inside the cluster ("cluster")
                              (Default: client).
  --class CLASS_NAME          Your application's main class (for Java / Scala apps).
  --name NAME                 A name of your application.
  --jars JARS                 Comma-separated list of local jars to include on the driver
                              and executor classpaths.
  --packages                  Comma-separated list of maven coordinates of jars to include
                              on the driver and executor classpaths. Will search the local
                              maven repo, then maven central and any additional remote
                              repositories given by --repositories. The format for the
                              coordinates should be groupId:artifactId:version.
  --exclude-packages          Comma-separated list of groupId:artifactId, to exclude while
                              resolving the dependencies provided in --packages to avoid
                              dependency conflicts.
  --repositories              Comma-separated list of additional remote repositories to
                              search for the maven coordinates given with --packages.
  --py-files PY_FILES         Comma-separated list of .zip, .egg, or .py files to place
                              on the PYTHONPATH for Python apps.
  --files FILES               Comma-separated list of files to be placed in the working
                              directory of each executor.

  --conf PROP=VALUE           Arbitrary Spark configuration property.
  --properties-file FILE      Path to a file from which to load extra properties. If not
                              specified, this will look for conf/spark-defaults.conf.

  --driver-memory MEM         Memory for driver (e.g. 1000M, 2G) (Default: 1024M).
  --driver-java-options       Extra Java options to pass to the driver.
  --driver-library-path       Extra library path entries to pass to the driver.
  --driver-class-path         Extra class path entries to pass to the driver. Note that
                              jars added with --jars are automatically included in the
                              classpath.

  --executor-memory MEM       Memory per executor (e.g. 1000M, 2G) (Default: 1G).

  --proxy-user NAME           User to impersonate when submitting the application.

  --help, -h                  Show this help message and exit
  --verbose, -v               Print additional debug output
  --version,                  Print the version of current Spark

 Spark standalone with cluster deploy mode only:
  --driver-cores NUM          Cores for driver (Default: 1).

 Spark standalone or Mesos with cluster deploy mode only:
  --supervise                 If given, restarts the driver on failure.
  --kill SUBMISSION_ID        If given, kills the driver specified.
  --status SUBMISSION_ID      If given, requests the status of the driver specified.

 Spark standalone and Mesos only:
  --total-executor-cores NUM  Total cores for all executors.

 Spark standalone and YARN only:
  --executor-cores NUM        Number of cores per executor. (Default: 1 in YARN mode,
                              or all available cores on the worker in standalone mode)

 YARN-only:
  --driver-cores NUM          Number of cores used by the driver, only in cluster mode
                              (Default: 1).
  --queue QUEUE_NAME          The YARN queue to submit to (Default: "default").
  --num-executors NUM         Number of executors to launch (Default: 2).
  --archives ARCHIVES         Comma separated list of archives to be extracted into the
                              working directory of each executor.
  --principal PRINCIPAL       Principal to be used to login to KDC, while running on
                              secure HDFS.
  --keytab KEYTAB             The full path to the file that contains the keytab for the
                              principal specified above. This keytab will be copied to
                              the node running the Application Master via the Secure
                              Distributed Cache, for renewing the login tickets and the
                              delegation tokens periodically.      

使用格式:  ./bin/spark-submit \   --class <main-class> \   --master <master-url> \   --deploy-mode <deploy-mode> \   --conf <key>=<value> \   ... # other options   <application-jar> \   [application-arguments]

以下針對各個參數進行簡要說明:

參數名 格式 參數說明 --master MASTER_URL 如spark://host:port, mesos://host:port, yarn,  yarn-cluster,yarn-client, local --deploy-mode DEPLOY_MODE Client或者master,預設是client --class CLASS_NAME 應用程式的主類 --name NAME 應用程式的名稱 --jars JARS 逗号分隔的本地jar包,包含在driver和executor的classpath下 --packages 包含在driver和executor的classpath下的jar包逗号分隔的”groupId:artifactId:version”清單 --exclude-packages 用逗号分隔的”groupId:artifactId”清單 --repositories 逗号分隔的遠端倉庫 --py-files PY_FILES 逗号分隔的”.zip”,”.egg”或者“.py”檔案,這些檔案放在python app的PYTHONPATH下面 --files FILES 逗号分隔的檔案,這些檔案放在每個executor的工作目錄下面 --conf PROP=VALUE 固定的spark配置屬性,預設是conf/spark-defaults.conf --properties-file FILE 加載額外屬性的檔案 --driver-memory MEM Driver記憶體,預設1024G --driver-java-options 傳給driver的額外的Java選項 --driver-library-path 傳給driver的額外的庫路徑 --driver-class-path 傳給driver的額外的類路徑 --executor-memory MEM 每個executor的記憶體,預設是1G --proxy-user NAME 模拟送出應用程式的使用者 --driver-cores NUM Driver的核數,預設是1。這個參數僅僅在standalone叢集deploy模式下使用 --supervise Driver失敗時,重新開機driver。在mesos或者standalone下使用 --verbose 列印debug資訊 --total-executor-cores NUM 所有executor總共的核數。僅僅在mesos或者standalone下使用 --executor-core NUM 每個executor的核數。在yarn或者standalone下使用 --driver-cores NUM Driver的核數,預設是1。在yarn叢集模式下使用 --queue QUEUE_NAME 隊列名稱。在yarn下使用,可為default --num-executors NUM 啟動的executor數量。預設為2。在yarn下使用

示例:

CMD="$SPARK_SUBMIT --driver-class-path $CLASSPATH \
  --files file:///path/conf/hochoy-kerberos.xml,/usr/lib/hochoy-table.xml \
  --properties-file $ROOT/conf/razor-spark.conf \
  --queue default \
  --driver-cores 4 \
  --driver-memory 1024M \
  --executor-cores 4 \
  --executor-memory 1024M \
  --num-executors 6 \
  --class $CLASS --master ${MASTER:-yarn} \
  --deploy-mode client \
  $ROOT/hochoy-spark-0.1-SNAPSHOT.jar ${@:2}"
  echo -e "$CMD"
  run "$CMD" &
           
# Run application locally on 8 cores(本地模式8核)
./bin/spark-submit \
  --class org.apache.spark.hochoy.SparkPi \
  --master local[8] \
  /path/to/examples.jar \
  100
# Run on a Spark standalone cluster in client deploy mode(standalone client模式)
./bin/spark-submit \
  --class org.apache.spark.hochoy.SparkPi \
  --master spark://ip:port \
  --executor-memory 20G \
  --total-executor-cores 100 \
  /path/to/examples.jar \
  1000
# Run on a Spark standalone cluster in cluster deploy mode with supervise(standalone cluster模式使用supervise)
./bin/spark-submit \
  --class org.apache.spark.examples.SparkPi \
  --master spark://207.184.161.138:7077 \
  --deploy-mode cluster \
  --supervise \
  --executor-memory 20G \
  --total-executor-cores 100 \
  /path/to/examples.jar \
  1000
# Run on a YARN cluster(YARN cluster模式)
export HADOOP_CONF_DIR=XXX
./bin/spark-submit \
  --class org.apache.spark.examples.SparkPi \
  --master yarn \
  --deploy-mode cluster \  # can be client for client mode
  --executor-memory 20G \
  --num-executors 50 \
  /path/to/examples.jar \
  1000
# Run on a Mesos cluster in cluster deploy mode with supervise(Mesos cluster模式使用supervise)
./bin/spark-submit \
  --class org.apache.spark.examples.SparkPi \
  --master mesos://207.184.161.138:7077 \
  --deploy-mode cluster \
  --supervise \
  --executor-memory 20G \
  --total-executor-cores 100 \
  http://path/to/examples.jar \
  1000
在公司使用最多的是spark on yarn模式,下面主要講spark on yarn
資源參數調優
所謂的Spark資源參數調優,其實主要就是對Spark運作過程中各個使用資源的地方,通過調節各種參數,來優化資源使用的效率,進而提升Spark作業的執行性能。
以下參數就是Spark中主要的資源參數,每個參數都對應着作業運作原理中的某個部分,我們同時也給出了一個調優的參考值。
num-executors
參數說明:
	該參數用于設定Spark作業總共要用多少個Executor程序來執行。Driver在向YARN叢集管理器申請資源時,YARN叢集管理器會盡可能按照你的設定來在
	叢集的各個工作節點上,啟動相應數量的Executor程序。這個參數非常之重要,如果不設定的話,預設隻會給你啟動少量的Executor程序,此時你的
	Spark作業的運作速度是非常慢的。
參數調優建議:
	每個Spark作業的運作一般設定50~100個左右的Executor程序比較合适,設定太少或太多的Executor程序都不好。設定的太少,無法充分利用叢集資源;
	設定的太多的話,大部分隊列可能無法給予充分的資源。
executor-memory
參數說明:
	該參數用于設定每個Executor程序的記憶體。Executor記憶體的大小,很多時候直接決定了Spark作業的性能,而且跟常見的JVM OOM異常,也有直接的關聯。
參數調優建議:
	每個Executor程序的記憶體設定4G~8G較為合适。但是這隻是一個參考值,具體的設定還是得根據不同部門的資源隊列來定。可以看看自己團隊的資源隊列
	的最大記憶體限制是多少,num-executors乘以executor-memory,是不能超過隊列的最大記憶體量的。此外,如果你是跟團隊裡其他人共享這個資源隊列,
	那麼申請的記憶體量最好不要超過資源隊列最大總記憶體的1/3~1/2,避免你自己的Spark作業占用了隊列所有的資源,導緻别的同學的作業無法運作。
executor-cores
參數說明:
	該參數用于設定每個Executor程序的CPU core數量。這個參數決定了每個Executor程序并行執行task線程的能力。因為每個CPU core同一時間隻能執行一個
	task線程,是以每個Executor程序的CPU core數量越多,越能夠快速地執行完配置設定給自己的所有task線程。
參數調優建議:
	Executor的CPU core數量設定為2~4個較為合适。同樣得根據不同部門的資源隊列來定,可以看看自己的資源隊列的最大CPU core限制是多少,再依據設定的
	Executor數量,來決定每個Executor程序可以配置設定到幾個CPU core。同樣建議,如果是跟他人共享這個隊列,那麼num-executors * executor-cores不要超過
	隊列總CPU core的1/3~1/2左右比較合适,也是避免影響其他同學的作業運作。
driver-memory
參數說明:
	該參數用于設定Driver程序的記憶體。
參數調優建議:
	Driver的記憶體通常來說不設定,或者設定1G左右應該就夠了。唯一需要注意的一點是,如果需要使用collect算子将RDD的資料全部拉取到Driver上進行處理,
	那麼必須確定Driver的記憶體足夠大,否則會出現OOM記憶體溢出的問題。
spark.default.parallelism
參數說明:
	該參數用于設定每個stage的預設task數量。這個參數極為重要,如果不設定可能會直接影響你的Spark作業性能。
參數調優建議:
	Spark作業的預設task數量為500~1000個較為合适。很多同學常犯的一個錯誤就是不去設定這個參數,那麼此時就會導緻Spark自己根據底層HDFS的block數量
	來設定task的數量,預設是一個HDFS block對應一個task。通常來說,Spark預設設定的數量是偏少的(比如就幾十個task),如果task數量偏少的話,就會
	導緻你前面設定好的Executor的參數都前功盡棄。試想一下,無論你的Executor程序有多少個,記憶體和CPU有多大,但是task隻有1個或者10個,那麼90%的
	Executor程序可能根本就沒有task執行,也就是白白浪費了資源!是以Spark官網建議的設定原則是,設定該參數為num-executors * executor-cores的2~3倍
	較為合适,比如Executor的總CPU core數量為300個,那麼設定1000個task是可以的,此時可以充分地利用Spark叢集的資源。
spark.storage.memoryFraction
參數說明:
	該參數用于設定RDD持久化資料在Executor記憶體中能占的比例,預設是0.6。也就是說,預設Executor 60%的記憶體,可以用來儲存持久化的RDD資料。根據你選擇
	的不同的持久化政策,如果記憶體不夠時,可能資料就不會持久化,或者資料會寫入磁盤。
參數調優建議:
	如果Spark作業中,有較多的RDD持久化操作,該參數的值可以适當提高一些,保證持久化的資料能夠容納在記憶體中。避免記憶體不夠緩存所有的資料,導緻資料隻
	能寫入磁盤中,降低了性能。但是如果Spark作業中的shuffle類操作比較多,而持久化操作比較少,那麼這個參數的值适當降低一些比較合适。此外,如果發現
	作業由于頻繁的gc導緻運作緩慢(通過spark web ui可以觀察到作業的gc耗時),意味着task執行使用者代碼的記憶體不夠用,那麼同樣建議調低這個參數的值。
spark.shuffle.memoryFraction
參數說明:
	該參數用于設定shuffle過程中一個task拉取到上個stage的task的輸出後,進行聚合操作時能夠使用的Executor記憶體的比例,預設是0.2。也就是說,Executor
	預設隻有20%的記憶體用來進行該操作。shuffle操作在進行聚合時,如果發現使用的記憶體超出了這個20%的限制,那麼多餘的資料就會溢寫到磁盤檔案中去,此時
	就會極大地降低性能。
參數調優建議:
	如果Spark作業中的RDD持久化操作較少,shuffle操作較多時,建議降低持久化操作的記憶體占比,提高shuffle操作的記憶體占比比例,避免shuffle過程中資料過多
	時記憶體不夠用,必須溢寫到磁盤上,降低了性能。此外,如果發現作業由于頻繁的gc導緻運作緩慢,意味着task執行使用者代碼的記憶體不夠用,那麼同樣建議調低
	這個參數的值。
資源參數的調優,沒有一個固定的值,需要根據自己的實際情況(包括Spark作業中的shuffle操作數量、RDD持久化操作數量以及spark web ui中顯示的作業gc情況),
合理地設定上述參數。
資源參數參考示例
以下是一份spark-submit指令的示例,大家可以參考一下,并根據自己的實際情況進行調節:
./bin/spark-submit \
  --master yarn-cluster \
  --num-executors 100 \
  --executor-memory 6G \
  --executor-cores 4 \
  --driver-memory 1G \
  --conf spark.default.parallelism=1000 \
  --conf spark.storage.memoryFraction=0.5 \
  --conf spark.shuffle.memoryFraction=0.3 \
           

繼續閱讀