进入Spark交互模式的命令是执行Spark安装目录下bin/spark-shell,下面我们来学习一下spark-shell 源码。
spark-shell 源码:
#!/usr/bin/env bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Shell script for starting the Spark Shell REPL
cygwin=false
case "$(uname)" in
CYGWIN*) cygwin=true;;
esac
# Enter posix mode for bash
set -o posix
if [ -z "${SPARK_HOME}" ]; then
source "$(dirname "$0")"/find-spark-home
fi
export _SPARK_CMD_USAGE="Usage: ./bin/spark-shell [options]"
# SPARK-4161: scala does not assume use of the java classpath,
# so we need to add the "-Dscala.usejavacp=true" flag manually. We
# do this specifically for the Spark shell because the scala REPL
# has its own class loader, and any additional classpath specified
# through spark.driver.extraClassPath is not automatically propagated.
SPARK_SUBMIT_OPTS="$SPARK_SUBMIT_OPTS -Dscala.usejavacp=true"
function main() {
if $cygwin; then
# Workaround for issue involving JLine and Cygwin
# (see http://sourceforge.net/p/jline/bugs/40/).
# If you're using the Mintty terminal emulator in Cygwin, may need to set the
# "Backspace sends ^H" setting in "Keys" section of the Mintty options
# (see https://github.com/sbt/sbt/issues/562).
stty -icanon min 1 -echo > /dev/null 2>&1
export SPARK_SUBMIT_OPTS="$SPARK_SUBMIT_OPTS -Djline.terminal=unix"
"${SPARK_HOME}"/bin/spark-submit --class org.apache.spark.repl.Main --name "Spark shell" "$@"
stty icanon echo > /dev/null 2>&1
else
export SPARK_SUBMIT_OPTS
"${SPARK_HOME}"/bin/spark-submit --class org.apache.spark.repl.Main --name "Spark shell" "$@"
fi
}
# Copy restore-TTY-on-exit functions from Scala script so spark-shell exits properly even in
# binary distribution of Spark where Scala is not installed
exit_status=127
saved_stty=""
# restore stty settings (echo in particular)
function restoreSttySettings() {
stty $saved_stty
saved_stty=""
}
function onExit() {
if [[ "$saved_stty" != "" ]]; then
restoreSttySettings
fi
exit $exit_status
}
# to reenable echo if we are interrupted before completing.
trap onExit INT
# save terminal settings
saved_stty=$(stty -g 2>/dev/null)
# clear on error so we don't later try to restore them
if [[ ! $? ]]; then
saved_stty=""
fi
main "$@"
# record the exit status lest it be overwritten:
# then reenable echo and propagate the code.
exit_status=$?
onExit
下面我们来分析spark-shell 都做了什么。
1、判断当前系统环境是否是 cygwin
case "$(uname)" in
CYGWIN*) cygwin=true;;
esac
先说说 Linux 里的 case 语句,case分支语句的格式如下:
case $变量名 in
模式1)
命令序列1
;;
模式2)
命令序列2
;;
*)
默认执行的命令序列 ;;
esac
case行尾必须为单词“in”,每一个模式必须以右括号“)”结束。
双分号“;;”表示命令序列结束。
匹配模式中可是使用方括号表示一个连续的范围,如[0-9];使用竖杠符号“|”表示或。
最后的“*)”表示默认模式,当使用前面的各种模式均无法匹配该变量时,将执行“*)”后
的命令序列,最后以 esac 结束。
$ uname
Linux
2、设置shell的模式为POSIX标准模式
set -o posix
不同的模式对于一些命令和操作不一样。
Posix : Portable Operating System Interface of Unix
它提供了操作系统的一套接口。
3、检测是否设置过SPARK_HOME环境变量
if [ -z "${SPARK_HOME}" ]; then
source "$(dirname "$0")"/find-spark-home
fi
if 条件表达式
if [ -f file ] 如果文件存在
if [ -d ... ] 如果目录存在
if [ -s file ] 如果文件存在且非空
if [ -r file ] 如果文件存在且可读
if [ -w file ] 如果文件存在且可写
if [ -x file ] 如果文件存在且可执行
if [ int1 -eq int2 ] 如果int1等于int2
if [ int1 -ne int2 ] 如果不等于
if [ int1 -ge int2 ] 如果>=
if [ int1 -gt int2 ] 如果>
if [ int1 -le int2 ] 如果<=
if [ int1 -lt int2 ] 如果<
If [ $a = $b ] 如果string1等于string2,字符串允许使用赋值号做等号
if [ $string1 != $string2 ] 如果string1不等于string2
if [ -n $string ] 如果string 非空(非0),返回0(true)
if [ -z $string ] 如果string 为空
if [ $sting ] 如果string 非空,返回0 (和-n类似) 条件表达式引用变量要带$
逻辑非 ! 条件表达式的相反
if [ ! 表达式 ]
if [ ! -d $num ] 如果不存在目录$num
逻辑与 –a 条件表达式的并列
if [ 表达式1 –a 表达式2 ]
逻辑或 -o 条件表达式的或
if [ 表达式1 –o 表达式2 ]
逻辑表达式
表达式与前面的= != -d –f –x -ne -eq -lt等合用
逻辑符号就正常的接其他表达式,没有任何括号( ),就是并列
if [ -z "$JHHOME" -a -d $HOME/$num ]
注意逻辑与-a与逻辑或-o很容易和其他字符串或文件的运算符号搞混了
最常见的赋值形式,赋值前对=两边的变量都进行评测
左边测变量是否为空,右边测目录(值)是否存在(值是否有效)
$(dirname "$0") // 获取当前脚本所在的路径
$# 是传给脚本的参数个数
$0 是脚本本身的名字
$1 是传递给该shell脚本的第一个参数
$2 是传递给该shell脚本的第二个参数
$@ 是传给脚本的所有参数的列表
$* 是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个
$$ 是脚本运行的当前进程ID号
$? 是显示最后命令的退出状态,0表示没有错误,其他表示有错误
通过 source 命令读入当前路径下 find-spark-home 文件并依次执行文件中的所有语句。
source "$(dirname "$0")"/find-spark-home
例如,当我们修改了/etc/profile文件,并想让它立刻生效,而不用重新登录,就可以使用source命令,如source /etc/profile。
4、导入 _SPARK_CMD_USAGE 变量
export _SPARK_CMD_USAGE="Usage: ./bin/spark-shell [options]"
export 命令
功能说明:设置或显示环境变量。(比如我们要用一个命令,但这个命令的执行文件不在当前目录,这样我们每次用的时候必须指定执行文件的目录,麻烦,在代码中先执行export,这个相当于告诉程序,执行某某东西时,需要的文件或什么东东在这些目录里)。
5、手动设置java classpath
SPARK_SUBMIT_OPTS="$SPARK_SUBMIT_OPTS -Dscala.usejavacp=true"
因为scala默认不会使用java classpath,因此这里需要手动设置一下,让scala使用java。
6、执行spark-submit,调用repl.Main
function main() {
if $cygwin; then
# Workaround for issue involving JLine and Cygwin
# (see http://sourceforge.net/p/jline/bugs/40/).
# If you're using the Mintty terminal emulator in Cygwin, may need to set the
# "Backspace sends ^H" setting in "Keys" section of the Mintty options
# (see https://github.com/sbt/sbt/issues/562).
## 如果是 cygwin 环境,-icanon:启用erase、kill、werase 和rprnt 等特殊字符;
## min 1 -echo:和 -icanon 配合使用,设置每次一完整读入的最小字符数为1;-echo 关闭回显
stty -icanon min 1 -echo > /dev/null 2>&1
export SPARK_SUBMIT_OPTS="$SPARK_SUBMIT_OPTS -Djline.terminal=unix"
"${SPARK_HOME}"/bin/spark-submit --class org.apache.spark.repl.Main --name "Spark shell" "$@"
stty icanon echo > /dev/null 2>&1
else
export SPARK_SUBMIT_OPTS
"${SPARK_HOME}"/bin/spark-submit --class org.apache.spark.repl.Main --name "Spark shell" "$@"
fi
}
上面的代码大体上做了三件事:
1) 捕获终端信号,执行退出方法,恢复一些操作
2) 保存终端配置,当cygwin时关闭回显,之后再恢复
3) 执行spark-submit,调用repl.Main
2>&1
几个基本符号及其含义
/dev/null 表示空设备文件
0 表示stdin标准输入
1 表示stdout标准输出
2 表示stderr标准错误
把标准出错重定向到标准输出,然后扔到/DEV/NULL下面去。通俗的说,就是把所有标准输出和标准出错都扔到垃圾桶里面。
7、捕获INT中断信号,然后执行onExit方法
onExit中判断是否恢复终端设置。
trap onExit INT
trap
命令支持捕获特定的信号,然后执行某个命令。常用的用法有:
trap "commands" signal-list 捕获到特定的信号,执行commands命令
trap signal-list 捕获特定的信号,停止当前进程
trap " " signal-list 捕获特定的信号,什么也不做
SIGHUP 终止进程 终端线路挂断
SIGINT 终止进程 中断进程
SIGQUIT 建立CORE文件 终止进程,并且生成core文件
SIGILL 建立CORE文件 非法指令
SIGTRAP 建立CORE文件 跟踪自陷
SIGBUS 建立CORE文件 总线错误
SIGSEGV 建立CORE文件 段非法错误
SIGFPE 建立CORE文件 浮点异常
SIGIOT 建立CORE文件 执行I/O自陷
SIGKILL 终止进程 杀死进程
SIGPIPE 终止进程 向一个没有读进程的管道写数据
SIGALARM 终止进程 计时器到时
SIGTERM 终止进程 软件终止信号
SIGSTOP 停止进程 非终端来的停止信号
SIGTSTP 停止进程 终端来的停止信号
SIGCONT 忽略信号 继续执行一个停止的进程
SIGURG 忽略信号 I/O紧急信号
SIGIO 忽略信号 描述符上可以进行I/O
SIGCHLD 忽略信号 当子进程停止或退出时通知父进程
SIGTTOU 停止进程 后台进程写终端
SIGTTIN 停止进程 后台进程读终端
SIGXGPU 终止进程 CPU时限超时
SIGXFSZ 终止进程 文件长度过长
SIGWINCH 忽略信号 窗口大小发生变化
SIGPROF 终止进程 统计分布图用计时器到时
SIGUSR1 终止进程 用户定义信号1
SIGUSR2 终止进程 用户定义信号2
SIGVTALRM 终止进程 虚拟计时器到时
最常用的信号有四个,SIGHUP,SIGINT,SIGQUIT,SIGTSTP
,使用的时候可以简写:
trap "" 1 2 3 24
或
trap "" HUP INT QUIT TSTP
8、保存当前的终端配置
-g:以stty可读方式打印当前的所有配置。
saved_stty=$(stty -g 2>/dev/null)
如果收到退出命令,就恢复stty状态:
if [[ ! $? ]]; then
saved_stty=""
fi
然后调用main方法,并传递所有的参数
main "$@"
,最后根据返回状态,判断是直接终端退出还是恢复之前的终端界面。
function main() {
if $cygwin; then
# Workaround for issue involving JLine and Cygwin
# (see http://sourceforge.net/p/jline/bugs/40/).
# If you're using the Mintty terminal emulator in Cygwin, may need to set the
# "Backspace sends ^H" setting in "Keys" section of the Mintty options
# (see https://github.com/sbt/sbt/issues/562).
stty -icanon min 1 -echo > /dev/null 2>&1
export SPARK_SUBMIT_OPTS="$SPARK_SUBMIT_OPTS -Djline.terminal=unix"
"${SPARK_HOME}"/bin/spark-submit --class org.apache.spark.repl.Main --name "Spark shell" "$@"
stty icanon echo > /dev/null 2>&1
else
export SPARK_SUBMIT_OPTS
"${SPARK_HOME}"/bin/spark-submit --class org.apache.spark.repl.Main --name "Spark shell" "$@"
fi
}
如果是cygwin,先关闭echo回显,设置读操作最少1个字符。然后启动spark-submit 执行
org.apache.spark.repl.Main
类,并设置应用的名字,传递参数。执行完成后,再开启echo回显。