Spark原始碼分析之Spark Shell(上)
https://www.cnblogs.com/xing901022/p/6412619.html
文中分析的spark版本為apache的spark-2.1.0-bin-hadoop2.7。
bin目錄結構:
-rwxr-xr-x. 1 bigdata bigdata 1089 Dec 15 2016 beeline -rw-r--r--. 1 bigdata bigdata 899 Dec 15 2016 beeline.cmd -rw-rw-r--. 1 bigdata bigdata 776 Sep 18 06:27 derby.log -rwxr-xr-x. 1 bigdata bigdata 1933 Dec 15 2016 find-spark-home -rw-r--r--. 1 bigdata bigdata 1909 Dec 15 2016 load-spark-env.cmd -rw-r--r--. 1 bigdata bigdata 2133 Dec 15 2016 load-spark-env.sh drwxrwxr-x. 5 bigdata bigdata 4096 Sep 18 06:27 metastore_db -rwxr-xr-x. 1 bigdata bigdata 2989 Dec 15 2016 pyspark -rw-r--r--. 1 bigdata bigdata 1493 Dec 15 2016 pyspark2.cmd -rw-r--r--. 1 bigdata bigdata 1002 Dec 15 2016 pyspark.cmd -rwxr-xr-x. 1 bigdata bigdata 1030 Dec 15 2016 run-example -rw-r--r--. 1 bigdata bigdata 988 Dec 15 2016 run-example.cmd -rwxr-xr-x. 1 bigdata bigdata 3116 Dec 15 2016 spark-class -rw-r--r--. 1 bigdata bigdata 2236 Dec 15 2016 spark-class2.cmd -rw-r--r--. 1 bigdata bigdata 1012 Dec 15 2016 spark-class.cmd -rwxr-xr-x. 1 bigdata bigdata 1039 Dec 15 2016 sparkR -rw-r--r--. 1 bigdata bigdata 1014 Dec 15 2016 sparkR2.cmd -rw-r--r--. 1 bigdata bigdata 1000 Dec 15 2016 sparkR.cmd -rwxr-xr-x. 1 bigdata bigdata 3017 Dec 15 2016 spark-shell -rw-r--r--. 1 bigdata bigdata 1530 Dec 15 2016 spark-shell2.cmd -rw-r--r--. 1 bigdata bigdata 1010 Dec 15 2016 spark-shell.cmd -rwxr-xr-x. 1 bigdata bigdata 1065 Dec 15 2016 spark-sql -rwxr-xr-x. 1 bigdata bigdata 1040 Dec 15 2016 spark-submit -rw-r--r--. 1 bigdata bigdata 1128 Dec 15 2016 spark-submit2.cmd -rw-r--r--. 1 bigdata bigdata 1012 Dec 15 2016 spark-submit.cmd
先來介紹一下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
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" " [email protected]"
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" "[email protected]"
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 " [email protected]"
# 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
source "$(dirname "$0")"/find-spark-home
fi
第一個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}是否為空的意思。
source命令用於呼叫另一個指令碼。
source "$(dirname "$0")"/find-spark-home
上面這句話整個的意思就是呼叫當前指令碼所在目錄中find-spark-home這個指令碼。
我們具體分析一下:
首先$0
是shell中的變數符號,類似的還有很多:
$# 是傳給指令碼的引數個數
$0 是指令碼本身的名字
$1 是傳遞給該shell指令碼的第一個引數
$2 是傳遞給該shell指令碼的第二個引數
[email protected] 是傳給指令碼的所有引數的列表
$* 是以一個單字串顯示所有向指令碼傳遞的引數,與位置變數不同,引數可超過9個
$$ 是指令碼執行的當前程序ID號
$? 是顯示最後命令的退出狀態,0表示沒有錯誤,其他表示有錯誤
最常用的應該是$0
和[email protected]。
在說說dirname命令,這個命令用於顯示某個檔案所在的路徑。比如我有一個檔案/home/xinghl/test/test1
,在test目錄中使用dirname test1
,就會返回:
[[email protected] test]# pwd
/home/xinghl/test
[[email protected] test]# ll
總用量 4
-rw-r--r-- 1 root root 27 2月 17 10:48 test1
[[email protected] test]# dirname test1
我們要的其實就是那個點,在linux中.
代表當前目錄。..
代表父目錄。
SPARK_SUBMIT_OPTS="$SPARK_SUBMIT_OPTS -Dscala.usejavacp=true"
因為scala預設不會使用java classpath,因此這裡需要手動設定一下,讓scala使用java。
就先介紹到這吧.....後面再介紹下,spark-shell視窗的原理。
作者:xingoo