构建一个由Master+Slave构成的Spark集群,Spark运行在集群中
1)进入spark安装目录下的conf文件夹
[atguigu@hadoop102 module]$ cd spark/conf/
2)修改配置文件名称
[atguigu@hadoop102 conf]$ mv slaves.template slaves
[atguigu@hadoop102 conf]$ mv spark-env.sh.template spark-env.sh
3)修改slave文件,添加work节点:
[atguigu@hadoop102 conf]$ vim slaves
hadoop102
hadoop103
hadoop104
4)修改spark-env.sh文件,添加如下配置:
[atguigu@hadoop102 conf]$ vim spark-env.sh
SPARK_MASTER_HOST=hadoop102
SPARK_MASTER_PORT=7077
5)分发spark包
[atguigu@hadoop102 module]$ xsync spark/
6)启动
[atguigu@hadoop102 spark]$ sbin/start-all.sh
(以下是脚本执行)
[atguigu@hadoop102 spark]$ util.sh
================atguigu@hadoop102================
3330 Jps
3238 Worker
3163 Master
================atguigu@hadoop103================
2966 Jps
2908 Worker
================atguigu@hadoop104================
2978 Worker
3036 Jps
网页查看:hadoop102:8080
注意:如果遇到 “JAVA_HOME not set” 异常,可以在sbin目录下的spark-config.sh 文件中加入如下配置:
export JAVA_HOME=XXXX
7)官方求PI案例
[atguigu@hadoop102 spark]$ bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master spark://hadoop102:7077 \
--executor-memory 1G \
--total-executor-cores 2 \
./examples/jars/spark-examples_2.11-2.1.1.jar \
100
8)启动spark shell
/opt/module/spark/bin/spark-shell \
--master spark://hadoop102:7077 \
--executor-memory 1g \
--total-executor-cores 2
参数:--master spark://hadoop102:7077指定要连接的集群的master
执行WordCount程序
scala>sc.textFile("input").flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_).collect
res0: Array[(String, Int)] = Array((hadoop,6), (oozie,3), (spark,3), (hive,3), (atguigu,3), (hbase,6))
scala>
1)修改spark-default.conf.template名称
[atguigu@hadoop102 conf]$ mv spark-defaults.conf.template spark-defaults.conf
2)修改spark-default.conf文件,开启Log:
[atguigu@hadoop102 conf]$ vi spark-defaults.conf
spark.eventLog.enabled true
spark.eventLog.dir hdfs://hadoop102:9000/directory
注意:HDFS上的目录需要提前存在。
[atguigu@hadoop102 hadoop]$ hadoop fs –mkdir /directory
3)修改spark-env.sh文件,添加如下配置:
[atguigu@hadoop102 conf]$ vi spark-env.sh
export SPARK_HISTORY_OPTS="-Dspark.history.ui.port=18080
-Dspark.history.retainedApplications=30
-Dspark.history.fs.logDirectory=hdfs://hadoop102:9000/directory"
参数描述:
spark.eventLog.dir:Application在运行过程中所有的信息均记录在该属性指定的路径下;
spark.history.ui.port=18080 WEBUI访问的端口号为18080
spark.history.fs.logDirectory=hdfs://hadoop102:9000/directory 配置了该属性后,在start-history-server.sh时就无需再显式的指定路径,Spark History Server页面只展示该指定路径下的信息
spark.history.retainedApplications=30指定保存Application历史记录的个数,如果超过这个值,旧的应用程序信息将被删除,这个是内存中的应用数,而不是页面上显示的应用数。
4)分发配置文件
[atguigu@hadoop102 conf]$ xsync spark-defaults.conf
[atguigu@hadoop102 conf]$ xsync spark-env.sh
5)启动历史服务
[atguigu@hadoop102 spark]$ sbin/start-history-server.sh
6)再次执行任务
[atguigu@hadoop102 spark]$ bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master spark://hadoop102:7077 \
--executor-memory 1G \
--total-executor-cores 2 \
./examples/jars/spark-examples_2.11-2.1.1.jar \
100
7)查看历史服务
hadoop102:18080
HA架构图
1)zookeeper正常安装并启动
2)修改spark-env.sh文件添加如下配置:
[atguigu@hadoop102 conf]$ vi spark-env.sh
注释掉如下内容:
#SPARK_MASTER_HOST=hadoop102
#SPARK_MASTER_PORT=7077
添加上如下内容:
export SPARK_DAEMON_JAVA_OPTS="
-Dspark.deploy.recoveryMode=ZOOKEEPER
-Dspark.deploy.zookeeper.url=hadoop102,hadoop103,hadoop104
-Dspark.deploy.zookeeper.dir=/spark"
3)分发配置文件
[atguigu@hadoop102 conf]$ xsync spark-env.sh
4)在hadoop102上启动全部节点
[atguigu@hadoop102 spark]$ sbin/start-all.sh
5)在hadoop103上单独启动master节点
[atguigu@hadoop103 spark]$ sbin/start-master.sh
6)spark HA集群访问
/opt/module/spark/bin/spark-shell \
--master spark://hadoop102:7077,hadoop103:7077 \
--executor-memory 2g \
--total-executor-cores 2