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

原文地址:https://www.cnblogs.com/qi-yuan-008/p/12199152.html

时间: 2024-10-15 00:22:34

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

Spark程序提交到Yarn集群时所遇异常

Exception 1:当我们将任务提交给Spark Yarn集群时,大多会出现以下异常,如下: 14/08/09 11:45:32 WARN component.AbstractLifeCycle: FAILED [email protected]:4040: java.net.BindException: Address already in use java.net.BindException: Address already in use at sun.nio.ch.Net.bind0(

Spark Submit提交应用程序

英文标题:Submitting Applications 英文原址:http://spark.apache.org/docs/latest/submitting-applications.html Spark Version:1.3.0 Spark根目录的bin目录下spark-submit脚本用于在集群上启动应用程序,它通过统一接口使用Spark所支持的所有集群管理器,因此无需特殊配置每一个应用程序. 1,绑定应用程序依赖 如果代码依赖于其它项目,为了将代码分发到Spark集群,就需要将这些依

jenkins~集群分发功能的具体实现

前一讲主要说了jenkins分发的好处<jenkins~集群分发功能和职责处理>,它可以让具体的节点干自己具体的事,比如windows环境下的节点,它只负责编译,发布windows的生态环境的项目:而linux节点主要负责和它相关的项目,如nodejs,.net core,java,php,python等等,你甚至可以做个mac节点,让它去编译打包你的IOS项目,听起来确实不错. 开始jenkins分发节点构建之旅 今天主要说的是建立分发节点的步骤,当然这也是大多人同学希望看到的文章,我们会以

在eclipse上提交任务到集群执行

win7下eclipse远程开发hadoop程序,分为两种: (1)运行[Run As] Java Application, 打包程序为jar,上传集群执行(这里不做解释) (2)运行[Run As] Run on Hadoop 重点来说说Run on Hadoop这种方式,搭建好eclipse远程开发环境,执行Run on Hadoop,程序成功了,心里窃喜,却发现是这个样子: 我明明设置job.setNumReduceTasks(6),最终本应该有6个reduce输出,怎么成了一个? 发现这

Spark学习之第一个程序打包、提交任务到集群

1.免秘钥登录配置: ssh-keygen cd .ssh touch authorized_keys cat id_rsa.pub > authorized_keys chmod 600 authorized_keys 2.环境工具 2.1环境 系统 urbuntu jdk 1.7.0_79 scala 2.10.4 hadoop 2.6.0 spark 1.6.2 2.2打包工具 IDEA + sbt1.2打包工具 3.打包 3.1安装插件 需要预先安装scala插件,点击File ->S

spark基于Zookeeper的HA集群重启

1.首先应该了解,spark集群基于Zookeeper的HA实现 当master挂了之后,会重新ElectLeader,不会影响Application的运行.默认的Zookeeper"/spark"目录下,会持久化driver,worker,master等信息. 2.Application需要重新submit的情况是在重启Driver所在的机器的时候,也就是说Driver一旦挂了,那么只有重新去提交Application这一途径. 3.重启步骤: 01,02,03三台master机器.

Spark 概述及其高可用集群部署

Spark入门 一. 学习目标 目标1:熟悉Spark相关概念 目标2:搭建一个Spark集群 二. Spark概述 2.1什么是Spark(官网:http://spark.apache.org) Spark是一种快速.通用.可扩展的大数据分析引擎,2009年诞生于加州大学伯克利分校AMPLab,2010年开源,2013年6月成为Apache孵化项目,2014年2月成为Apache顶级项目.目前,Spark生态系统已经发展成为一个包含多个子项目的集合,其中包含SparkSQL.Spark Str

Spark的Master和Worker集群启动的源码分析

基于spark1.3.1的源码进行分析 spark master启动源码分析 1.在start-master.sh调用master的main方法,main方法调用 def main(argStrings: Array[String]) { SignalLogger.register(log) val conf = new SparkConf val args = new MasterArguments(argStrings, conf) val (actorSystem, _, _, _) =

Spark 1.6.1分布式集群环境搭建

一.软件准备 scala-2.11.8.tgz spark-1.6.1-bin-hadoop2.6.tgz 二.Scala 安装 1.master 机器 (1)下载 scala-2.11.8.tgz, 解压到 /opt 目录下,即: /opt/scala-2.11.8. (2)修改 scala-2.11.8 目录所属用户和用户组. ? 1 sudo chown -R hadoop:hadoop scala-2.11.8 (3)修改环境变量文件 .bashrc , 添加以下内容. ? 1 2 3