終於開始看Spark源碼了,先從最常用的spark shell腳本開始吧。不要覺得一個啟動腳本有什麼東東,其實裡面還是有很多知識點的。另外,從啟動腳本入手,是尋找代碼入口最簡單的方法,很多開源框架,其實都可以通過這種方式來尋找源碼入口。 先來介紹一下Spark shell是什麼? Spark she ...
終於開始看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視窗的原理。