此方法通过反射的方式,生成用户指定的入口类或是Spark的内置类,由于生成的类可能是实现了SparkApplication
接口的子类抑或是一个自定义的类,因此需要根据这两种情况分析选择是直接执行生成类的start(...)
方法间接调用生成类的main(...)
方法,同时传递所有解析到的spark参数及需要应用接收的各个args。
* Run the main method of the child class using the provided launch environment.
* Note that this main class will not be the one provided by the user if we're
* running cluster deploy mode or python applications.
private def runMain(
childArgs: Seq[String],
childClasspath: Seq[String],
sparkConf: SparkConf,
childMainClass: String,
verbose: Boolean): Unit = {
var mainClass: Class[_] = null
try {
mainClass = Utils.classForName(childMainClass)
} catch {
case e: ClassNotFoundException =>
logWarning(s"Failed to load $childMainClass.", e)
if (childMainClass.contains("thriftserver")) {
logInfo(s"Failed to load main class $childMainClass.")
logInfo("You need to build Spark with -Phive and -Phive-thriftserver.")
throw new SparkUserAppException(CLASS_NOT_FOUND_EXIT_STATUS)
case e: NoClassDefFoundError =>
logWarning(s"Failed to load $childMainClass: ${e.getMessage()}")
if (e.getMessage.contains("org/apache/hadoop/hive")) {
logInfo(s"Failed to load hive class.")
logInfo("You need to build Spark with -Phive and -Phive-thriftserver.")
throw new SparkUserAppException(CLASS_NOT_FOUND_EXIT_STATUS)
val app: SparkApplication = if (classOf[SparkApplication].isAssignableFrom(mainClass)) {
mainClass.newInstance().asInstanceOf[SparkApplication]
} else {
if (classOf[scala.App].isAssignableFrom(mainClass)) {
logWarning("Subclasses of scala.App may not work correctly. Use a main() method instead.")
new JavaMainApplication(mainClass)
@tailrec
def findCause(t: Throwable): Throwable = t match {
case e: UndeclaredThrowableException =>
if (e.getCause() != null) findCause(e.getCause()) else e
case e: InvocationTargetException =>
if (e.getCause() != null) findCause(e.getCause()) else e
case e: Throwable =>
try {
app.start(childArgs.toArray, sparkConf)
} catch {
case t: Throwable =>
throw findCause(t)
到此,整个通过spark-submit命令提交任务的流程已简单剖析完毕,更详细的内容读者可以自行查看源码。
下一篇浅析创建Kubernetes任务的流程。
spark-submit方式提交应用启动脚本文件# 命令行提交Spark应用样例:#./bin/spark-submit \# --class com.imooc.spark.Test.TestOfSparkContext2 \# --conf spark.master spark://localhost:7077 \# --master local[2] \# /home/hadoop/data/test-jar/sql-1.0.jar arg1 arg2#if [ -z "${
spark-submit 命令使用详解
spark-submit 用户打包 Spark 应用程序并部署到 Spark 支持的集群管理气上,命令语法如下:
spark-submit [options] <python file> [app arguments]
app arguments 是传递给应用程序的参数,常用的命令行参数如下所示:
–master: 设置主节点 URL 的参数...
启动spark-shell或执行spark-submit失败的问题
楼主搭建了一套CDH大数据平台,其中包括Spark2服务,近来在终端启动spark-shell以及利用spark-submit提交任务时,分别报以下错误:
[root@cdh0 ~]# spark-shell
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/fs/FSDataInputStream
at org.apac
String[] args = new String[]{"--jars",
"/root/kafka_2.12-0.10.2.0/libs/kafka-clients-0.10.2.0.jar,/root/spark-streaming-kafka-0-10_2.11-2.1.0.jar",
"--driver-class-path",
"/roo
--master
MASTER_URL, 可 以 是 spark://host:port, mesos://host:port, yarn, yarn-cluster,yarn-client, local
--deploy-mode
DEPLOY_MODE, Driver 程序运行的地方,client 或者 cluster,默认是client。
Spark-submit脚本提交任务时最简易的命令格式如下:
./bin/spark-submit \
--master spark://localhost:7077 \
任务包 任务参数
而实际开发中用的一般是如下的格式
./bin/spark-submit \
--master yarn \
--deploy-mode cluster \
--driver-memory 1g \
--executor-memory 1g \
--executor-cores 11
今天主要分析一下Spark源码中提交任务脚本的处理逻辑,从spark-submit一步步深入进去看看任务提交的整体流程,首先看一下整体的流程概要图:
接下来按照图中结构出发一步步看源码:spark-submit#!/usr/bin/env bash#
# Licensed to the Apache Software Foundation (ASF) under one or more
# con
./spark-submit --master spark://node1:7077 --deploy-mode cluster --class org.apache.spark.examples.SparkPi ../examples/jars/spark-examples_2.11-2.4.0.jar 100
其中 spark - submit 脚...
大数据实验教学系统使用spark-submit工具提交Spark作业对于数据的批处理,通常采用编写程序、打.jar包提交给集群来执行,这需要使用Spark自带的spark-submit工具。
一般的部署策略是在一个网关机器上提交应用程序,这个机器和Worker机器部署在一个网络中(例如,Standalone模式的集群中的Master节点)。在此部署策略中,client模式更为合适,client模式中的driver直接跟spark-submit进程一起启动,spark-submit进程在此扮演集群中一个c
原来程序是将所有jar打包到libs目录下,然后运行生成好的run.sh。现在要使用spark-submit将它提交到spark上运行。几经波折之后,终于圆满完成。
首先遇到的问题是如何使用gradle将工程打包成可执行的jar文件。这个问题网上已有答案,就是使用插件
"com.github.johnrengelman.shadow"。gradle的配置如下:
apply plugin: '
其中,`[options]`是可选的命令行选项,可以用来设置Spark应用程序的配置参数,例如`--master`用于设置Spark集群的主节点地址,`--num-executors`用于设置执行器的数量等等。`<app jar | python file>`是必须的参数,用于指定要提交的应用程序的jar包或Python文件。`[app arguments]`是可选的应用程序参数,用于传递给应用程序的命令行参数。
例如,以下命令将提交一个Java应用程序到Spark集群中运行:
spark2-submit --class com.example.MyApp --master yarn --num-executors 10 myapp.jar arg1 arg2
该命令指定了应用程序的主类为`com.example.MyApp`,使用YARN作为集群管理器,设置了10个执行器,并传递了两个应用程序参数`arg1`和`arg2`。
总之,spark2-submit是Spark应用程序提交的重要工具,可以通过命令行选项来配置应用程序的运行环境和参数,方便地将应用程序提交到Spark集群中运行。