终于开始看Spark源码了,先从最常用的spark-shell脚本开始吧。不要觉得一个启动脚本有什么东东,其实里面还是有很多知识点的。另外,从启动脚本入手,是寻找代码入口最简单的方法,很多开源框架,其实都可以通过这种方式来寻找源码入口。

先来介绍一下Spark-shell是什么?

Spark-shell是提供给用户即时交互的一个命令窗口,你可以在里面编写spark代码,然后根据你的命令立即进行运算。这种东西也被叫做REPL,(Read-Eval-Print Loop)交互式开发环境。

先来粗略的看一眼,其实没有多少代码:

#!/usr/bin/env bash

# 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
export SPARK_HOME="$(cd "`dirname "$0"`"/..; pwd)"
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-submit,后续会再分析一下spark-submit的作用(它里面会调用spark-class,这才是执行方法的最终执行者,前面都是传参而已)。

最前面的

cygwin=false
case "`uname`" in
CYGWIN*) cygwin=true;;
esac

这个在很多的启动脚本中都可以看到,是检查你的系统是否属于cygwin。使用了uname命令,这个命令通常用于查询系统的名字或者内核版本号

uname可以查看操作系统的名字, 详情参考 man uname.直接输入uname,一般显示Linux; 使用uname -r 可以查看内核版本; 使用uname -a 可以查看所有的信息

set -o posix

设置shell的模式为POSIX标准模式,不同的模式对于一些命令和操作不一样。Posix : Portable Operating System Interface of Unix它提供了操作系统的一套接口。

if [ -z "${SPARK_HOME}" ]; then
export SPARK_HOME="$(cd "`dirname "$0"`"/..; pwd)"
fi

这句在很多启动脚本中也比较常见,即获取应用的主目录。因为一般的应用都是这样的

app主目录/bin 启动脚本
app主目录/lib 相关jar
app主目录/logs 日志

而启动脚本一般放在bin下面,所以应用的主目录就是bin的父目录而已。

第一个if语句if [ -z "${SPARK_HOME}" ]; then用于检测是否设置过SPARK_HOME环境变量。

在shell里面条件表达式有非常多的用法,比如:

# 文件表达式
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类似)

所以上面的那句判断,就是检查${SPARK_HOME}是否为空的意思。

export命令用于在当前的登陆中,设置某个环境变量,如果注销,设置就失效了。所以如果你想要永久配置环境变量,还是得去/etc/profile里面去看。

所以就应该能明白了,export SPARK_HOME="$(cd "dirname "$0""/..; pwd)"这句话就是设置SPARK_HOME环境变量的。那么里面那一坨是干嘛的呢?咱们一点一点看。

首先$0是shell中的变量符号,类似的还有很多:

$# 是传给脚本的参数个数
$0 是脚本本身的名字
$1 是传递给该shell脚本的第一个参数
$2 是传递给该shell脚本的第二个参数
$@ 是传给脚本的所有参数的列表
$* 是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个
$$ 是脚本运行的当前进程ID号
$? 是显示最后命令的退出状态,0表示没有错误,其他表示有错误

最常用的应该是$0$@.

在说说dirname命令,这个命令用于显示某个文件所在的路径。比如我有一个文件/home/xinghl/test/test1,在test目录中使用dirname test1,就会返回:

[root@localnode3 test]# pwd
/home/xinghl/test
[root@localnode3 test]# ll
总用量 4
-rw-r--r-- 1 root root 27 2月 17 10:48 test1
[root@localnode3 test]# dirname test1
.

我们要的其实就是那个点,在linux中.代表当前目录。..代表父目录。因此cd ./.. 就是进入父目录的意思。

后面的pwd是显示当前路径。

整个连起来就是:

1 先获取当前路径
2 脚本路径进入到应用主目录
3 pwd显示路径,赋值给SPARK_HOME

有人就会问了,这不多此一举么?干嘛不直接写cd ..,这是因为你在哪执行spark-shell是不一定的。因此cd命令直接cd ..会根据你的目录而改变。举个例子:

[root@localnode3 test]# pwd
/home/xinghl/test
[root@localnode3 test]# cat test.sh
cd ./..
echo `pwd`
[root@localnode3 test]# sh test.sh
/home/xinghl
[root@localnode3 test]# cd ..
[root@localnode3 xinghl]# sh test/test.sh
/home

看出来作用了吧!

SPARK_SUBMIT_OPTS="$SPARK_SUBMIT_OPTS -Dscala.usejavacp=true"

因为scala默认不会使用java classpath,因此这里需要手动设置一下,让scala使用java。

就先介绍到这吧.....后面再介绍下,spark-shell窗口的原理。

最新文章

  1. Programming Language A 学习笔记(二)
  2. 剑指Offer 整数中1出现的次数(从1到n整数中1出现的次数)
  3. 2015-10-27 js
  4. 【转】火火火火火!看HomeKit如何改变物联网和智能家居?
  5. C#识别图片上的数字
  6. HTML介绍JS
  7. CGI杂谈
  8. HDU 3346 Lucky Number
  9. 在Python中用Request库模拟登录(一):字幕库(无加密,无验证码)
  10. C#中的is和as
  11. 设置下载文件路径 &amp; 获取接口结尾名称。
  12. WordPress数据结构分析
  13. Codeforces 1154E Two Teams
  14. solr源码分析之数据导入DataImporter追溯。
  15. Visualbox在UEFI模式下无法正常引导
  16. Github SSH key 的配置
  17. [UE4]射击起点、终点的计算方法
  18. XDocument 使用
  19. msdn 中MethodBase.Invoke 方法 介绍中的坑
  20. JavaScript push()函数追加数组数据

热门文章

  1. display属性及inline-block值(可用来布局)
  2. BZOJ 2209: [Jsoi2011]括号序列 [splay 括号]
  3. linux平台很nice的工具
  4. Insert Select
  5. 数据结构-String、char
  6. 从损坏的wt文件中恢复出WiredTiger集合
  7. LDA 线性判别分析
  8. Struts2验证
  9. 手动写一个Servlet
  10. Angular - - angular.injector、angular.module