spark-submit提交任务到集群,分发虚拟环境和第三方包

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 park.pyspark.driver.python=/root/anaconda3/envs/my_test_env/bin/python3.7 main.py

然后即可运行。

6. 当然,shell脚本特别牛的,可以直接分发shell命令到各台机器,每次提交作业时就执行shell脚本来分发某台机器上的虚拟环境,然后执行,经过测试也是可行的。

 

参考:

https://blog.csdn.net/qwezhaohaihong/article/details/84772060

https://blog.csdn.net/Yuan_CSDF/article/details/81773174

https://blog.csdn.net/u010569893/article/details/96438379

上一篇:Validate表单验证判断是否验证通过


下一篇:FutureTask 的submit源码