天天看點

[Spark]Spark 應用程式部署工具spark-submit

1. 簡介

Spark的bin目錄中的spark-submit腳本用于啟動叢集上的應用程式。 可以通過統一的接口使用Spark所有支援的叢集管理器,是以不必為每個叢集管理器專門配置你的應用程式(It can use all of Spark’s supported cluster managers through a uniform interface so you don’t have to configure your application specially for each one)。

https://note.youdao.com/md/?file=%2Fyws%2Fapi%2Fpersonal%2Ffile%2FWEB63f90752a772edcdfcf3f478775f997c%3Fmethod%3Ddownload%26read%3Dtrue#2-%E8%AF%AD%E6%B3%95 2. 文法

xiaosi@yoona:~/opt/spark-2.1.0-bin-hadoop2.7$ 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://...]
Usage: spark-submit run-example [options] example-class [example args]

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.
                              This argument does not work with --principal / --keytab.

  --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).
                              If dynamic allocation is enabled, the initial number of
                              executors will be at least NUM.
  --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.           

https://note.youdao.com/md/?file=%2Fyws%2Fapi%2Fpersonal%2Ffile%2FWEB63f90752a772edcdfcf3f478775f997c%3Fmethod%3Ddownload%26read%3Dtrue#3-%E6%8D%86%E7%BB%91%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F%E7%9A%84%E4%BE%9D%E8%B5%96%E5%85%B3%E7%B3%BB 3. 捆綁應用程式的依賴關系

如果你的代碼依賴于其他項目,則需要将它們與應用程式一起打包,以便将代碼分發到Spark叢集上。為此,請建立一個包含代碼及其依賴關系的程式集jar(或 Uber jar)。sbt和Maven都有裝配插件。建立jar時,将Spark和Hadoop列出作為需要提供的依賴關系; 這些不需要捆綁,因為它們在運作時由叢集管理器提供。一旦你有一個jar,你可以調用bin/ spark-submit腳本,如下所示,同時傳遞你的jar作為參數。

對于Python,您可以使用spark-submit的--py-files參數來添加.py,.zip或.egg檔案以與應用程式一起分發。如果你依賴于多個Python檔案,我們建議将它們打包成一個.zip或.egg檔案。

https://note.youdao.com/md/?file=%2Fyws%2Fapi%2Fpersonal%2Ffile%2FWEB63f90752a772edcdfcf3f478775f997c%3Fmethod%3Ddownload%26read%3Dtrue#4-%E4%BD%BF%E7%94%A8spark-submit%E5%90%AF%E5%8A%A8%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F 4. 使用spark-submit啟動應用程式

一旦使用者應用程式打包成功後,可以使用bin/spark-submit腳本啟動應用程式。此腳本負責設定Spark的 classpath 及其依賴關系,并且可以支援不同叢集管理器和部署模式(Spark所支援的):

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

一些常用的選項:

  • --class 應用程式入口 (例如:com.sjf.open.spark.Java.JavaWordCount 包含包名的全路徑名稱)
  • --master 叢集的主URL (例如:spark://23.195.26.187:7077)
  • --deploy-mode 部署driver運作的地方,client或者cluster
  • application-jar 包含應用程式和所有依賴關系的jar路徑。 URL必須在叢集内部全局可見,例如,所有節點上存在的hdfs://路徑或file://路徑。
  • application-arguments 傳遞給主類main方法的參數(如果有的話)

如果你送出應用程式的機器遠離工作節點機器(例如在筆記本電腦本地送出),則通常使用叢集模式來最小化drivers和executors之間的網絡延遲。 目前,對于Python應用程式而言,在獨立模式上不支援叢集模式。

對于Python應用程式,隻需在<application-jar>位置傳遞一個.py檔案來代替JAR,然後使用--py-files參數ca将Python .zip,.egg或.py檔案添加到搜尋路徑。

有幾個可用選項是特定用于叢集管理器。例如,對于具有叢集部署模式的Spark獨立叢集,可以指定--supervise參數以確定如果driver以非零退出而失敗,則自動重新啟動。如果要列舉spark-submit所有可用選項,可以使用spark-submit --help指令來檢視。以下是常見選項的幾個示例:

# 在本地運作 8 核
./bin/spark-submit \
  --class org.apache.spark.examples.SparkPi \
  --master local[8] \
  /path/to/examples.jar \
  100

# 以用戶端部署模式在Spark獨立叢集上運作
./bin/spark-submit \
  --class org.apache.spark.examples.SparkPi \
  --master spark://207.184.161.138:7077 \
  --executor-memory 20G \
  --total-executor-cores 100 \
  /path/to/examples.jar \
  1000

# 在叢集部署模式下使用supervise在Spark獨立叢集上運作
./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

# 在 YARN 叢集上運作
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

# 在 Spark 獨立叢集上運作Python程式
./bin/spark-submit \
  --master spark://207.184.161.138:7077 \
  examples/src/main/python/pi.py \
  1000

# 在叢集部署模式下使用supervise在Mesos叢集上運作
./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           

https://note.youdao.com/md/?file=%2Fyws%2Fapi%2Fpersonal%2Ffile%2FWEB63f90752a772edcdfcf3f478775f997c%3Fmethod%3Ddownload%26read%3Dtrue#5-master-urls 5. Master Urls

傳遞給Spark的master url 可以采用如下格式:

Master URL 描述
local 使用一個工作線程本地運作Spark
local[K] 使用K個工作線程在本地運作Spark(理想情況下,将其設定為機器上的核數)。
local[*] 使用與計算機上的邏輯核數一樣多的工作線程在本地運作Spark。
spark://HOST:PORT 連接配接到給定的Spark獨立叢集主機。 端口必須是主機配置可使用的端口,預設情況下為7077。
mesos://HOST:PORT 連接配接到給定的Mesos叢集。 端口必須是主機配置可使用的端口,預設為5050。 或者,對于使用ZooKeeper的Mesos叢集,請使用mesos://zk:// .... 要使用--deploy-mode cluster 送出。
yarn 以用戶端模式還是以叢集模式連接配接到YARN群集具體取決于--deploy-mode的值。 可以根據HADOOP_CONF_DIR或YARN_CONF_DIR變量找到叢集位置

https://note.youdao.com/md/?file=%2Fyws%2Fapi%2Fpersonal%2Ffile%2FWEB63f90752a772edcdfcf3f478775f997c%3Fmethod%3Ddownload%26read%3Dtrue#6-%E4%BB%8E%E6%96%87%E4%BB%B6%E5%8A%A0%E8%BD%BD%E9%85%8D%E7%BD%AE 6. 從檔案加載配置

spark-submit腳本可以從properties檔案加載預設Spark配置選項,并将它們傳遞到應用程式。預設情況下,spark 從spark目錄下的conf/spark-defaults.conf配置檔案中讀取配置選項。有關更多詳細資訊,請閱讀

加載預設配置

以這種方式加載預設Spark配置可以避免在spark-submit上添加配置選項。例如,如果預設配置檔案中設定了spark.master屬性,則可以安全地從spark-submit中省略--master參數。一般來說,在SparkConf上顯式設定的配置選項擁有最高優先級,然後是傳遞到spark-submit的配置選項,然後是預設配置檔案中的配置選項。

如果不清楚配置選項來自哪裡,可以通過使用--verbose選項運作spark-submit列印出細粒度的調試資訊。

https://note.youdao.com/md/?file=%2Fyws%2Fapi%2Fpersonal%2Ffile%2FWEB63f90752a772edcdfcf3f478775f997c%3Fmethod%3Ddownload%26read%3Dtrue#7-%E9%AB%98%E7%BA%A7%E4%BE%9D%E8%B5%96%E7%AE%A1%E7%90%86 7. 進階依賴管理

使用spark-submit時,應用程式jar以及包含在-jars選項中的jar将自動傳輸到叢集。在--jars之後提供的URL清單必須用逗号分隔。 該清單會包含在driver和 executor 的classpath中。 目錄擴充不能與--jars一起使用。

Spark使用如下URL方案以不同政策傳播傳送jar:

  • file : 絕對路徑和file:/ URI 由driver 的HTTP檔案伺服器提供,每個executor從driver HTTP伺服器拉取檔案。
  • hdfs :, http :, https :, ftp: 正如你希望的一樣,這些從URI拉取檔案和JAR
  • local: 以local:/開頭的URI應該作為每個工作節點上的本地檔案存在。 這意味着不會産生網絡IO,适用于推送大檔案/ JAR到每個工作線程或通過NFS,GlusterFS等方式共享這些大檔案/jar。

請注意,JAR和檔案被複制到執行器節點上每個SparkContext的工作目錄(Note that JARs and files are copied to the working directory for each SparkContext on the executor nodes)。随着時間的推移,這可能會占用大量的空間,需要定時清理。使用YARN,清理會自動執行;使用Spark獨立叢集,可以使用spark.worker.cleanup.appDataTtl屬性配置自動清理。

使用者還可以通過用--packages提供逗号分隔的maven坐标清單來包含任何其他依賴項。使用此指令時将處理所有傳遞依賴性。可以使用配置選項--repositories以逗号分隔的方式添加其他存儲庫(或SBT中的解析器)。pyspark,spark-shell和spark-submit都可以使用這些指令來包含Spark Packages。

對于Python,等價的--py-files選項可用于将.egg,.zip和.py庫分發給執行程式。