spark-submit提交任務的參數很多:
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.
1. 但是,一般提交作業到本地 [local] 模式,則很簡單:
直接:spark-submit *.py即可,當然,其中是要配置好該機器的python解釋器位置:在spark的安裝目錄下,有一個spark-env.sh文件,例如:/opt/spark/spark-2.1.1-bin-hadoop2.7/conf/spark-env.sh
在其中設置環境變量PYSPARK_PYTHON,例如添加:export PYSPARK_PYTHON=/usr/bin/python3
2. 但是如果是集群模式,則其他機器也要在同樣的目錄下,安裝python以及所需的包,當機器很多時,管理起來會比較麻煩,因為每次都可能裝新的包。—— 於是產生了一個方法:通過分發包文件,來執行python代碼,也就是一般所說的sc.addPyFile(r'/root/test_words/lib_words.zip') 方法,具體見:https://www.cnblogs.com/qi-yuan-008/p/11877805.html,當然這只對簡單的獨立包有效。
3. 然而,上述方法在遇到復雜依賴包的時候,例如:numpy和pandas,它們需要與系統的C extension擴展進行編譯才能使用,但是打包的時候無法打包所需的C擴展文件,於是會造成各種錯誤,其結果就是分發的包不適用,大多會出現類似:ImportError: C extension: No module named 'pandas._libs.tslib' not built. 的錯誤。
4. 這時,就需要一種新的方法來解決這個問題,簡單又實用。
網上流傳最多的一種方法就是:打包虛擬環境 —— 即通過anaconda構建虛擬環境,然后打包分發該環境,或者通過virtualenv來打包分發虛擬環境,然后將python解釋器指向所分發的虛擬環境中的python即可。
具體設置是:(首先已經將打包的anaconda3環境,例如:anaconda3/envs/my_env目錄,將my_env文件夾打包成zip文件,明名為my_env.zip)
## anaconda方法 spark-submit \ --master yarn \ --deploy-mode cluster \ --num-executors 4 \ --executor-memory 2G \ --archives hdfs:///user/xxx/lib/my_env.zip#my_env\ --conf spark.yarn.appMasterEnv.PYSPARK_PYTHON=./my_env/my_env/bin/python3 \
main.py
或者(例如:將virtualenv環境打包成my_env.zip)
## virtualenv方法 spark-submit \ --master yarn \ --deploy-mode cluster \ --num-executors 4 \ --executor-memory 2G \ --archives hdfs:///user/xxx/spark/my_env.zip#my_env\ –conf spark.pyspark.driver.python=hdfs:///user/xxx/spark/my_env/bin/python3 \ –conf spark.pyspark.python=./my_env/my_env/bin/python3 \ main.py
但是這兩種方法都是通過yarn模式管理的,本人尚未嘗試。
5. 本人嘗試成功的是(其實這個不算是分發虛擬環境了,因為每台機器都裝了包,只不過用anaconda來管理了):
首先:在所有機器上,安裝anaconda3,新建虛擬環境,然后在該虛擬環境下(例如:my_test_env)通過requirements安裝所需的庫(pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple),幾台機器全部一致(包括anaconda3的路徑和安裝的庫)。
然后不需要修改原先的spark-env的環境變量,例如:PYSPARK_PYTHON=/usr/bin/python3,也就是可以保持原樣,這樣不會破壞集群的配置。
其次:通過其中某個機器由spark-submit提交作業時,使用參數如下:
spark-submit --conf spark.pyspark.driver.python=/root/anaconda3/envs/my_test_env/bin/python3.7 --conf spark.pyspark.python=/root/anaconda3/envs/my_test_env/bin/python3.7 main.py
然后即可運行。
6. 附加:如果上述依然不行,可以在程序中添加:os.environ["PYSPARK_PYTHON"]="/root/anaconda3/envs/my_test_env/bin/python3.7" (改成anaconda3中的python路徑),然后再根據上述步驟嘗試。
參考:
https://blog.csdn.net/qwezhaohaihong/article/details/84772060