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)。 2. 文法
xiaosi@yoona:~/opt/spark-2.1.0-bin-hadoop2.7$ spark-submit --helpUsage: 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.
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檔案。 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
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變數找到叢集位置 |
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列印出細粒度的調試資訊。 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