spark學習、下載、編譯、安裝、運行

Apache Spark™是用於大規模數據處理的統一分析引擎。

下載

官網-download。
官網下載

編譯

  • 編譯方式
    • Maven編譯
    • SBT編譯
    • 打包編譯make-distribution.sh

選擇Maven方式編譯

注意版本要求

spark的編譯對maven,java版本有要求,下載並解壓相應版本的Maven和Java。

(a)

#配置JAVA_HOME
$ sudo vi /etc/profile
export JAVA_HOME=/XXX/XXX/jdkX.X.X_XX
export PATH=$PATH:$JAVA_HOME/bin

編輯退出後,使用source /etc/profile使之生效;

#如果遇到不能加載當前版本的問題
rpm -qa|grep jdk
rpm -e --nodeps jdk版本
which java 刪除/usr/bin/java

(b)

$ sudo vi /etc/profile
export MAVEN_HOME=
export PATH=$PATH:$MAVEN_HOME/bin
#configure Maven to use more memory than usual by setting MAVEN_OPTS
export MAVEN_OPTS="-Xmx2g -XX:MaxPermSize=1024M -XX:ReservedCodeCacheSize=1024M"

​ PS:

<!--ReservedCodeCacheSize是可選的,但是不寫的話可能會出錯-->
[INFO] Compiling 203 Scala sources and 9 Java sources to /Users/me/Development/spark/core/target/scala-2.12/classes...
[ERROR] Java heap space -> [Help 1]

編輯退出後,使用source /eytc/profile使之生效;

在/etc/resolv.conf:添加如下內容

nameserver 8.8.8.8
nameserver 8.8.4.4

spark/dev/make-distribution.sh中添加如下內容,加快編譯速度。


#Spark的版本
VERSION=
#Scala的版本
SCALA_VERSION=
#Hadoop的版本
SPARK_HADOOP_VERSION=
#支持spark on hive
SPARK_HIVE=1

編譯時網絡要外網連接

$./dev/make-distribution.sh --name custom-spark --tgz -Phadoop-X.X -Phive -Phive-thriftserver  -Pyarn

#編譯完成之後解壓

tar -zxf spark-X.X.X-bin-custom-spark.tgz -C /opt/modules/

Scala安裝

官網中描述對於Sacla的版本也是有要求的,將相應版本的Scala下載解壓到指定目錄。

配置環境變量

$ sudo vi /etc/profile
export SCALA_HOME=/XXX/XXX/scala-X.XX.X
export PATH=$PATH:$SCALA_HOME/bin
$ source /etc/profile

運行

運行成功的log
訪問Spark context web UI,查看相關服務的運行情況。

Spark運行模式之Standalone:

  • Standalone(Spark自身的集羣管理)

    需要安裝JDK、Scala、Hadoop、Spark Standalone(只需要在集羣中每個節點上安裝一個Spark的編譯版本)

spark 集羣架構圖

$./sbin/start-master.sh:啓動一個standalone master服務,一旦啓動,master將打印spark://HOST:PORTURL,這個URL可以連接workers,或者作爲"master"參數傳遞給SparkContext,也可以在master’s的web UI中發現這個URL。

$./sbin/start-slave.sh <master-spark-URL>:啓動workers,並連接master。

使用腳本啓動Spark standalone 集羣,需要在Spark目錄下創建conf/slaves文件,並在文件中寫入計劃運行Spark workers機器的hostname,如果該文件不存在,默認啓動Local模式。注意:master機器可以無密鑰連接每一個worker。

#slaves:一行一個
worker1 所在的hotname
worker2 所在的hotname

那麼可以在Spark master上運行以下腳本:

#Starts a master instance on the machine the script is executed on.
$ sbin/start-master.sh
#Starts a slave instance on each machine specified in the conf/slaves file.
$ sbin/start-slaves.sh  
#Starts a slave instance on the machine the script is executed on.
$ sbin/start-slave.sh
#Starts both a master and a number of slaves as described above.
$ sbin/start-all.sh
 #Stops the master that was started via the sbin/start-master.sh script.
$ sbin/stop-master.sh
#Stops all slave instances on the machines specified in the conf/slaves file.
$ sbin/stop-slaves.sh 
 #Stops both the master and the slaves as described above
$ sbin/stop-all.sh

通過conf/spark-env.sh進一步配置集羣,可以參考conf/spark-env.sh.template,然後將該文件複製到所有worker機器上。

#spark-env.sh
export JAVA_HOME=/XXX/XXX/jdkX.X.X_XX
export SCALA_HOME=/XXX/XXX/scala-X.XX.X
#配置這一項時,hadoop集羣得啓動
export HADOOP_CONF_DIR=/opt/modules/hadoop-X.X.X/etc/hadoop
export SPARK_CONF_DIR=/opt/modules/spark-X.X.X/conf
#綁定master,填寫hostname or IP address
export SPARK_MASTER_HOST=
#默認是7077
export SPARK_MASTER_PORT=7077
#master web UI的端口,默認是8080
export SPARK_MASTER_WEBUI_PORT=8080
#允許Spark 程序使用的內核總數
export SPARK_WORKER_CORES=1
#saprk程序使用的內存總數
export SPARK_WORKER_MEMORY=1g
#Spark worker啓動的端口,默認random
export SPARK_WORKER_PORT=7078
#worker web UI的端口8081
export SPARK_WORKER_WEBUI_PORT=8081

將應用程序連接到集羣,只需要將master的URL(spark://IP:PORT)傳遞給SparkContext構造器。運行一個交互Spark shell:$./bin/saprk-shell --master spark://IP:ROOT

spark-submit腳本提交編譯的Spark應用程序到集羣中。對於standalone集羣,Spark有兩種deploy modes:
1.client模式( the driver is launched in the same process as the client that submits the application )
2.cluster模式( the driver is launched from one of the Worker processes inside the cluster, and the client process exits as soon as it fulfills its responsibility of submitting the application without waiting for the application to finish )。
如果你的應用程序通過Spark submit運行,則應用程序自動分發給所有的worker節點,應用程序所依賴的jar包,通過–jars jar1,jar2

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章