继承上文“spark-core_01: $SPARK_HOME/bin/spark-shell脚本分析”
一,$SPARK_HOME/bin/spark-submit
################################################
#从spark-shell调用之后,传进来--class org.apache.spark.repl.Main --name "Spark shell" --master spark://luyl152:7077
#先检测spark_home,然后去调用spark_home/bin/spark-class 会将org.apache.spark.deploy.SparkSubmit作为第一个参数,
#----- 会执行脚本spark-class org.apache.spark.deploy.SparkSubmit --class org.apache.spark.repl.Main --name"Spark shell" --master spark://luyl152:7077
#####################################
#!/usr/bin/envbash
if [-z "${SPARK_HOME}" ]; then
export SPARK_HOME="$(cd "`dirname"$0"`"/..; pwd)"
fi
#disable randomized hash for string in Python 3.3+
exportPYTHONHASHSEED=0
#exec 执行完面的命令,exec 命令,是创建一个新的进程,只不过这个进程与前一个进程的ID是一样的。这样,原来的脚本剩余的部分代码就不能执行了,因为相当于换了一个进程。
exec"${SPARK_HOME}"/bin/spark-class org.apache.spark.deploy.SparkSubmit" $@"
二、$SPARK_HOME/bin/spark-class
###########################################################################
#--如果是spark-shell从spark-submit脚本传进来如下参数:
org.apache.spark.deploy.SparkSubmit --classorg.apache.spark.repl.Main --name "Spark shell" --masterspark://luyl152:7077
#如果自己的application则直接执行spark-submit 脚本传入自己的--class等参数信息就可以
###########################################################################
#!/usr/bin/envbash
#还是判断了一下SPARK_HOME环境变量是否存在
if [-z "${SPARK_HOME}" ]; then
export SPARK_HOME="$(cd "`dirname"$0"`"/..; pwd)"
fi
#---执行shell,不加“. 和空格”也能执行:配置一些环境变量,它会将conf/spark-env.sh中的环境变量加载进来:
. "${SPARK_HOME}"/bin/load-spark-env.sh
# ---Find the java binary 如果有java_home环境变量会将java_home/bin/java给RUNNER
#if [ -n str ] 表示当串的长度大于0时为真
if [-n "${JAVA_HOME}" ]; then
RUNNER="${JAVA_HOME}/bin/java"
else
#---command -v 和which的功能一样
if [ `command -v java` ]; then
RUNNER="java"
else
echo "JAVA_HOME is not set">&2
exit 1
fi
fi
#--- Find assembly jar : 会先找spark_home/RELESE文本是否存在,如果存在将spark_home/lib目录给变量ASSEMBLY_DIR
SPARK_ASSEMBLY_JAR=
if [-f "${SPARK_HOME}/RELEASE" ]; then
ASSEMBLY_DIR="${SPARK_HOME}/lib"
else
ASSEMBLY_DIR="${SPARK_HOME}/assembly/target/scala-$SPARK_SCALA_VERSION"
fi
#---ls -1与ls -l的区别在于ls -1只会返回文件名,没有文件类型,大小,日期等信息。num_jars返回spark-assembly的jar有多少个
GREP_OPTIONS=
num_jars="$(ls-1 "$ASSEMBLY_DIR" | grep "^spark-assembly.*hadoop.*\.jar$"| wc -l)"
#---如果$num_jars为0,会报错并退出
if ["$num_jars" -eq "0" -a -z "$SPARK_ASSEMBLY_JAR"-a "$SPARK_PREPEND_CLASSES" != "1" ]; then
echo "Failed to find Spark assembly in$ASSEMBLY_DIR." 1>&2
echo "You need to build Spark beforerunning this program." 1>&2
exit 1
fi
if [-d "$ASSEMBLY_DIR" ]; then
#---ls 后面加 || true和不加效果一样,还是会返回这个串的:spark-assembly-1.6.0-hadoop2.6.0.jar
ASSEMBLY_JARS="$(ls -1"$ASSEMBLY_DIR" | grep "^spark-assembly.*hadoop.*\.jar$" ||true)"
#---这个$num_jars不能大于1,否则会退出
if [ "$num_jars" -gt "1"]; then
echo "Found multiple Spark assemblyjars in $ASSEMBLY_DIR:" 1>&2
echo "$ASSEMBLY_JARS" 1>&2
echo "Please remove all but onejar." 1>&2
exit 1
fi
fi
#---将spark_home/lib/spark-assembly-1.6.0-hadoop2.6.0.jar给变量SPARK_ASSEMBLY_JAR
SPARK_ASSEMBLY_JAR="${ASSEMBLY_DIR}/${ASSEMBLY_JARS}"
#---还是将spark_home/lib/spark-assembly-1.6.0-hadoop2.6.0.jar赋给别一个变量LAUNCH_CLASSPATH
LAUNCH_CLASSPATH="$SPARK_ASSEMBLY_JAR"
#Add the launcher build dir to the classpath if requested.
#---if [ -n str ] 表示当串的长度大于0时为真
if [-n "$SPARK_PREPEND_CLASSES" ]; then
LAUNCH_CLASSPATH="${SPARK_HOME}/launcher/target/scala-$SPARK_SCALA_VERSION/classes:$LAUNCH_CLASSPATH"
fi
#--- 将spark_home/lib/spark-assembly-1.6.0-hadoop2.6.0.jar值给全局变量_SPARK_ASSEMBLY
export_SPARK_ASSEMBLY="$SPARK_ASSEMBLY_JAR"
#For tests
if[[ -n "$SPARK_TESTING" ]]; then
unset YARN_CONF_DIR
unset HADOOP_CONF_DIR
fi
#The launcher library will print arguments separated by a NULL character, toallow arguments with
#characters that would be otherwise interpreted by the shell. Read that in awhile loop, populating
# anarray that will be used to exec the final command.
#######################################################
#启动程序库将打印由空字符分隔的参数,以允许带有由shell解释的字符的参数。 在while循环中读取它,填充将用于执行最终命令的数组。
#如果要调试输入如下$JAVA_OPTS环境变量即可
#export JAVA_OPTS="$JAVA_OPTS -Xdebug-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
#如果想调试org.apache.spark.launcher.Main,需要将$JAVA_OPTS放在$RUNNER后面就可以了,
#如果想直接调用SparkSubmit.scala可以将$JAVA_OPTS放在 launcher.Main的后面
#-----spark-class先执行org.apache.spark.launcher.Main,由launcher.Main来解析脚本参数,然后由Main方法将打印的结果给while语句,再次执行SparkSubmit类中的main方法;
#小括号有,数组或执行的命令的意思
#java -cp spark_home/lib/spark-assembly-1.6.0-hadoop2.6.0.jarorg.apache.spark.launcher.Main org.apache.spark.deploy.SparkSubmit --class org.apache.spark.repl.Main--name "Spark shell" --master spark://luyl152:7077
#######################################################
CMD=()
whileIFS= read -d '' -r ARG; do
echo "===========>"$ARG
CMD+=("$ARG")
done< <("$RUNNER" -cp "$LAUNCH_CLASSPATH"org.apache.spark.launcher.Main "$@")
exec"${CMD[@]}"
#launcher.Main返回的数据存储到CMD数组中,可以通过打印查看$ARG里面的内容,实际上就是laucher.Main里面的main打印的参数
[root@luyl155 bin]# ./spark-shell --masterspark://luyl152:7077,luyl153:7077,luyl154:7077 --verbose /tool/jarDir/maven_scala-1.0-SNAPSHOT.jar
#ARG打印的内容如下,这样看来${CMD[@]}就是让 “java -cp 执行第三行包中类即SparkSubmit, 使用jvm参数(即第四,五,六,七行),然后执行SparkSubmit 后面的是SparkSubmit参数是 --class 等内容
===========>/usr/local/java/jdk1.8.0_91/bin/java
===========>-cp
===========>/data/spark-1.6.0-bin-hadoop2.6/conf/:/data/spark-1.6.0-bin-hadoop2.6/lib/spark-assembly-1.6.0-hadoop2.6.0.jar:
/data/spark-1.6.0-bin-hadoop2.6/lib/datanucleus-api-jdo-3.2.6.jar:/data/spark-1.6.0-bin-hadoop2.6/lib/datanucleus-rdbms-3.2.9.jar:
/data/spark-1.6.0-bin-hadoop2.6/lib/datanucleus-core-3.2.10.jar:/data/hadoop-2.6.5/etc/hadoop/
===========>-Xms1g
===========>-Xmx1g
===========>-Xdebug
===========>-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005
===========>org.apache.spark.deploy.SparkSubmit
===========>--class
===========>org.apache.spark.repl.Main
===========>--name
===========>Sparkshell
===========>--master
===========>spark://luyl152:7077,luyl153:7077,luyl154:7077
===========>--verbose
===========>/tool/jarDir/maven_scala-1.0-SNAPSHOT.jar
===========>spark://luyl152:7077,luyl153:7077,luyl154:7077
接下来分析一下org.apache.spark.launcher.Main源码