7、spark的生产应用提交脚本spark-submit

一、通过查询命令 spark-submit --help 来查看提交任务时有哪些选项可以用。

Options:说明备注【个人翻译和根据使用经验备注,有错误欢迎支持】
  --master MASTER_URL         spark://host:port, mesos://host:port, yarn,
 k8s://https://host:port, or local (Default: local[*]).
常用local本地模式、yarn集群模式 
  --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).

驱动程序是本地客户端client启动还是集群cluster上的工作节点启动.

如果是cluster模式,Yarn集群会管理driver进程,application创建后,client客户端就可以退出了。

如果是client模式,driver进程会跑在client客户端进程中,Yarn只负责保证执行节点的资源,并不会管理master节点。

  --class CLASS_NAME    Your application's main class (for Java / Scala apps).Java/Scala脚本的main class
  --name NAME      A name of your application.给应用一个名称
  --jars JARS      Comma-separated list of jars to include on the driver and executor classpaths.逗号分隔的jar包列表,会加载到驱动、执行节点的路径上
  --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.逗号分隔的maven坐标下的package包列表,会加载到驱动、执行节点的路径上。会搜索本地的maven资源库或远程资源池来加载。
  --exclude-packages          Comma-separated list of groupId:artifactId, to exclude while
resolving the dependencies provided in --packages to avoid
dependency conflicts.
逗号分隔的package包,在解析依赖的时候会排除不解析,防止依赖冲突。
  --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.逗号分隔的.zip , .egg, .py文件列表 
  --files FILES               Comma-separated list of files to be placed in the working directory of each executor. File paths of these files in executors can be accessed via SparkFiles.get(fileName).

逗号分隔的文件列表,替换工作节点路径下的文件。文件可以通过SparkFiles.get(fileName)获取

【注:这里文件其实会被加载存放到工作节点路径下,也不用使用SparkFiles.get(fileName)方式读取,直接读文件名即可】

  --conf, -c 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.spark-submit --help 获取命令行帮助
  --verbose, -v               Print additional debug output. 
  --version,                  Print the version of current Spark.spark-submit -version 查看当前版本号 
 Cluster deploy mode only:  只适用于集群部署模式的命令
  --driver-cores NUM          Number of cores used by the driver, only in cluster mode (Default: 1). 
 Spark standalone or Mesos with cluster deploy mode only:  
  --supervise                 If given, restarts the driver on failure. 
 Spark standalone, Mesos or K8s with cluster deploy mode only:  
  --kill SUBMISSION_ID        If given, kills the driver specified. 
  --status SUBMISSION_ID      If given, requests the status of the driver specified. 
 Spark standalone, Mesos and Kubernetes only:  
  --total-executor-cores NUM  Total cores for all executors. 
 Spark standalone, YARN and Kubernetes only:  
  --executor-cores NUM        Number of cores used by each executor. (Default: 1 in YARN and K8S modes, or all available cores on the worker in standalone mode). 
 Spark on YARN and Kubernetes only: 适用于Yarn和Kubernetes部署模式的命令
  --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.执行节点个数 
  --principal PRINCIPAL       Principal to be used to login to KDC. 
  --keytab KEYTAB             The full path to the file that contains the keytab for the principal specified above. 
 Spark on YARN only:  只适用于Yarn部署模式的命令
  --queue QUEUE_NAME          The YARN queue to submit to (Default: "default"). 队列名称
  --archives ARCHIVES         Comma separated list of archives to be extracted into the working directory of each executor. 

二、scala脚本spark-submit

1、yarn集群模式

1.1 spark-submit 命令模版

spark-submit --class TestClass
--master yarn \
--queue ${指定队列名称} \
--deploy-mode client \
--driver-memory 1G \
--conf spark.driver.maxResultSize=1G \
--driver-cores 2 \
--num-executors 4 \
--executor-cores 4 \
--executor-memory 16G \
--conf spark.dynamicAllocation.enabled=true \
--conf spark.shuffle.service.enabled=true \
--conf spark.sql.shuffle.partitions=6400 \
--conf spark.default.parallelism=6400 \
--conf spark.storage.memoryfraction=0.4 \
--conf spark.shuffle.memoryFraction=0.4 \
--conf spark.blacklist.enabled=true \
--conf spark.speculation=true \
--conf spark.hadoop.hive.exec.orc.split.strategy=ETL \
--name scala_test \
AtestSparkApplication.jar

1.2 object脚本示例

 

2、local本地模式

2.1 spark-submit 命令模版

spark-submit --class TestClass
--master local \
--deploy-mode client \
--driver-memory 1G \
--conf spark.driver.maxResultSize=1G \
--executor-memory 16G \
--conf spark.dynamicAllocation.enabled=true \
--conf spark.shuffle.service.enabled=true \
--conf spark.storage.memoryfraction=0.4 \
--conf spark.shuffle.memoryFraction=0.4 \
--conf spark.blacklist.enabled=true \
--conf spark.speculation=true \
--name scala_test \
AtestSparkApplication.jar

2.2 object脚本示例

 

三、python脚本spark-submit

1、yarn集群模式

1.1 spark-submit 命令模版

(1)一个python脚本,无任何其他依赖文件 的情况

spark-submit \
 --master yarn \
 --queue ${这是集群的队列} \
 --deploy-mode client \
 --driver-memory 4G \
 --driver-cores 4 \
 --executor-memory 8G \
 --executor-cores 4 \
 --num-executors 100 \
 --conf spark.default.parallelism=1600 \
 --name "spark_demo_yarn" \
 pyspark_example_yarn.py 

(2)一个python脚本,加上一个/多个 txtfile的情况

(3)一个python脚本,加上一个/多个 依赖python脚本的情况

 

1.2 脚本示例: pyspark_example_yarn.py 

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function


if __name__ == '__main__':

    from pyspark.sql import SparkSession
    spark = SparkSession.builder \
                .appName("Word Count") \
                .config("spark.some.config.option", "some-value") \
                .enableHiveSupport() \
                .getOrCreate()

    df = spark.sql("""
            SELECT
	            COUNT(a.user_id)
            FROM
	            (
		            SELECT
			            user_id
		            FROM
			            app.app_purchase_table
		            WHERE
			            dt >= "2019-01-01"
			            AND dt <= "2020-12-31"
			            AND sku_code IN(700052, 721057)
		            GROUP BY
			            user_id
                )
                a
            """)
    df.show()

2、local本地模式

2.1 spark-submit 命令模版

spark-submit \
 --master local \
 --deploy-mode client \
 --name "spark_demo_local" \
 pyspark_example_local.py 

2.2 脚本示例:  pyspark_example_local.py 

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function


if __name__ == '__main__':

    from pyspark.sql import SparkSession
    spark = SparkSession.builder \
                .appName("Word Count") \
                .config("spark.some.config.option", "some-value") \
                .enableHiveSupport() \
                .getOrCreate()

    print(spark.range(5000).where("id > 500").selectExpr("sum(id)").collect())
    spark.range(500).where("id > 400").show()

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这个错误提示意味着你的系统中没有安装 Apache Spark 或者没有将其添加到系统的环境变量中。你需要先安装 Apache Spark 并将其添加到系统的 PATH 环境变量中,才能在命令行中使用 spark-submit 命令。 ### 回答2: 问题简述: 当我们在使用spark-submit命令时,有可能会遇到“spark-submit:未找到命令”的错误提示,这是什么原因?如何解决呢? 问题分析: 对于这个问题,我们先来看一下spark-submit命令的用途和语法: spark-submit脚本提交应用程序的主要方式。它可以在命令行上使用,直接提交一个应用程序或一个应用程序的JAR包,它会在指定的集群上启动一个应用程序,并将JAR包上传到并行的计算集群中。spark-submit脚本可以与普通的Java包(JAR)库一起使用,支持使用各种命令行参数和选项。对于不是Java或Scala编写的应用程序,需要使用支持的语言的其他特定的打包和提交方法。 语法格式: spark-submit \ --class <main-class> \ --master <master-url> \ --deploy-mode <deploy-mode> \ --conf <key>=<value> \ ... # other options <application-jar> \ [application-arguments] 根据以上语法格式,我们可以初步判断出“spark-submit:未找到命令”一般是由以下几个原因导致的: 1.路径不正确:如果我们不在spark安装路径下,那么就需要将spark的bin目录添加到系统的PATH环境变量中。或者我们在使用spark-submit时,没有正确指定spark的安装路径。 2.未安装Spark:我们没有安装Spark或者Spark安装出现了问题,找不到spark-submit命令。 3.集群变量设置问题:很多时候,我们在配置Spark时需要配置一系列环境变量等配置信息,如果这些变量存在问题,也可能导致寻找不到spark-submit命令。 解决方法: 1.查看路径是否正确:首先要保证当前所在路径下可以找到Spark的安装路径,可以使用以下命令获得spark安装路径: echo $SPARK_HOME 如果输出为空,则说明需要设置环境变量。或者可以使用命令: apt-cache search spark 来查找本地是否已安装Spark。 2.设置环境变量:如上述原因,只需要将spark的bin路径添加到系统的PATH环境变量中即可。例如: export PATH=$PATH:/usr/local/spark/bin 3.重新安装Spark:如果无法找到spark-submit命令,则说明可能是spark安装出现了问题,我们可以重新安装Spark来解决此问题。 4.检查其他环境变量:如果环境变量没有设置正确,则也可能会导致找不到spark-submit命令。可以进一步检查环境变量是否设置正确。例如: export HADOOP_CONF_DIR=/etc/hadoop/conf export YARN_CONF_DIR=/etc/hadoop/conf 总结: “spark-submit:未找到命令”这个问题,可能有很多原因,我们需要根据具体情况进行分析,找到问题所在,再作出相应的解决方法。以上几种方法可以帮助解决此问题,望各位读者掌握。 ### 回答3: 当我们在Linux或Mac OS X的终端中输入“spark-submit”命令时,如果提示“command not found”或“未找到命令”,这意味着我们所在的环境没有找到这个命令,或者我们没有安装或配置Apache Spark。要解决这个问题,我们需要按照以下步骤操作: 1. 确认Spark是否安装 首先,我们需要确认Spark是否已经安装。我们可以在终端中输入“spark-shell”命令,如果能够正常启动Spark的Scala shell,则说明Spark已经安装,但是我们可能没有将Spark的bin目录添加到PATH中。 2. 检查环境变量$PATH 我们需要检查环境变量$PATH是否包含Spark的bin目录。我们可以在终端中输入“echo $PATH”命令,查看PATH变量的值。如果PATH中没有包含Spark的bin目录,则需要将其添加到PATH中。我们可以编辑~/.bash_profile文件(如果使用的是bash shell),并在其中添加以下行: export PATH=$PATH:/path/to/spark/bin 其中,/path/to/spark应该是Spark安装的路径。编辑完成后,需要执行以下命令使更改生效: source ~/.bash_profile 然后,我们可以使用“echo $PATH”命令检查PATH是否包含Spark的bin目录。 3. 检查spark-submit文件是否存在 我们还要检查我们的Spark安装目录中是否有spark-submit文件。Spark的bin目录应该包含spark-submit文件,如果该文件不存在,则需要重新安装Spark或检查安装目录是否正确。 4. 检查Spark版本 最后,我们需要检查我们正在使用的Spark版本是否支持spark-submit命令。一些较早的Spark版本可能不支持spark-submit,我们需要升级到较新的Spark版本。 总之,当我们在终端中输入“spark-submit”命令时,如果提示“command not found”或“未找到命令”,我们需要检查环境变量$PATH是否包含Spark的bin目录,检查Spark安装目录中是否存在spark-submit文件,以及检查我们所使用的Spark版本是否支持spark-submit命令。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值