本篇文章为大家展示了如何进行spark 2.2.0 Standalone安装及wordCount演示,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
前提:安装好hadoop集群,
一、 scala 安装
1、下载 scala 安装包 :https://d3kbcqa49mib13.cloudfront.net/spark-2.2.0-bin-hadoop2.7.tgz
2、上传 scala 安装包
[root@hadp-master local]# pwd
/usr/local
[root@hadp-master local]# ll scala-2.11.7.tgz
-rw-r–r–. 1 root root 28460530 Feb 25 03:53 scala-2.11.7.tgz
3、 解压并删除安装包
[root@hadp-master local]# tar -zxvf scala-2.11.7.tgz
[root@hadp-master local]# rm -rf scala-2.11.7.tgz
4、 配置环境变量
vi /etc/profile 添加如下
export SCALA_HOME=/usr/local/scala-2.11.7
export PATH=$PATH:$SCALA_HOME/bin
5、 生效,查看scala版本
[root@hadp-master local]# scala -version
Scala code runner version 2.11.7 — Copyright 2002-2013, LAMP/EPFL
6、 其他主机配置scala环境
[root@hadp-master local]# scp -r /usr/local/scala-2.11.7/ hadp-node1:/usr/local/
[root@hadp-master local]# scp -r /usr/local/scala-2.11.7/ hadp-node2:/usr/local/
[root@hadp-master local]# scp -r /etc/profile hadp-node1:/etc/profile
profile 100% 2414 2.4KB/s 00:00
[root@hadp-master local]# scp -r /etc/profile hadp-node2:/etc/profile
profile 100% 2414 2.4KB/s 00:00
二、 spark 安装
1、 下载 spark 安装包,上传
[root@hadp-master local]# pwd
/usr/local
[root@hadp-master local]# ll spark-2.2.0-bin-hadoop2.7.tgz
-rw-r–r–. 1 root root 203728858 Feb 25 04:20 spark-2.2.0-bin-hadoop2.7.tgz
2、 解压
[root@hadp-master local]# tar -zxvf spark-2.2.0-bin-hadoop2.7.tgz
3、 配置环境变量
vi /etc/profile 添加如下内容
export SPARK_HOME=/usr/local/spark-2.2.0-bin-hadoop2.7
export PATH=$PATH:$SPARK_HOME/bin
4、配置Spark环境
[root@hadp-master local]# cd spark-2.2.0-bin-hadoop2.7/conf/
[root@hadp-master conf]# ll
total 32
-rw-r–r–. 1 500 500 996 Jul 1 2017 docker.properties.template
-rw-r–r–. 1 500 500 1105 Jul 1 2017 fairscheduler.xml.template
-rw-r–r–. 1 500 500 2025 Jul 1 2017 log4j.properties.template
-rw-r–r–. 1 500 500 7313 Jul 1 2017 metrics.properties.template
-rw-r–r–. 1 500 500 865 Jul 1 2017 slaves.template
-rw-r–r–. 1 500 500 1292 Jul 1 2017 spark-defaults.conf.template
-rwxr-xr-x. 1 500 500 3699 Jul 1 2017 spark-env.sh.template
4.1
[root@hadp-master conf]# cp spark-env.sh.template spark-env.sh
[root@hadp-master conf]# vi spark-env.sh 末尾添加如下:
export JAVA_HOME=/usr/local/jdk1.8.0_131
export SCALA_HOME=/usr/local/scala-2.11.7
export HADOOP_HOME=/usr/local/hadoop/hadoop-2.7.4/
export HADOOP_CONF_DIR=/usr/local/hadoop/hadoop-2.7.4/etc/hadoop
export SPARK_MASTER_IP=hadp-master
export SPARK_WORKER_MEMORY=1g
export SPARK_WORKER_CORES=2
export SPARK_WORKER_INSTANCES=1
变量说明
– JAVA_HOME:Java安装目录
– SCALA_HOME:Scala安装目录
– HADOOP_HOME:hadoop安装目录
– HADOOP_CONF_DIR:hadoop集群的配置文件的目录
– SPARK_MASTER_IP:spark集群的Master节点的ip地址
– SPARK_WORKER_MEMORY:每个worker节点能够最大分配给exectors的内存大小
– SPARK_WORKER_CORES:每个worker节点所占有的CPU核数目
– SPARK_WORKER_INSTANCES:每台机器上开启的worker节点的数目
4.2
[root@hadp-master conf]# cp slaves.template slaves
[root@hadp-master conf]# vi slaves 添加如下
# A Spark Worker will be started on each of the machines listed below.
hadp-node1
hadp-node2
4.3
[root@hadp-master local]# scp -r spark-2.2.0-bin-hadoop2.7/ hadp-node1:/usr/local
[root@hadp-master local]# scp -r spark-2.2.0-bin-hadoop2.7/ hadp-node2:/usr/local
[root@hadp-master local]# scp /etc/profile hadp-node1:/etc/
profile 100% 2492 2.4KB/s 00:00
[root@hadp-master local]# scp /etc/profile hadp-node2:/etc/
profile 100% 2492 2.4KB/s 00:00
5、启动Spark集群
5.1
因为我们只需要使用hadoop的HDFS文件系统,所以我们并不用把hadoop全部功能都启动。
[root@hadp-master sbin]# pwd
/usr/local/hadoop/hadoop-2.7.4/sbin
[root@hadp-master sbin]# ./start-dfs.sh
Starting namenodes on [hadp-master]
hadp-master: starting namenode, logging to /usr/local/hadoop/hadoop-2.7.4/logs/hadoop-root-namenode-hadp-master.out
hadp-node2: starting datanode, logging to /usr/local/hadoop/hadoop-2.7.4/logs/hadoop-root-datanode-hadp-node2.out
hadp-node1: starting datanode, logging to /usr/local/hadoop/hadoop-2.7.4/logs/hadoop-root-datanode-hadp-node1.out
Starting secondary namenodes [0.0.0.0]
0.0.0.0: starting secondarynamenode, logging to /usr/local/hadoop/hadoop-2.7.4/logs/hadoop-root-secondarynamenode-hadp-master.out
[root@hadp-master sbin]# jps
4914 NameNode
5235 Jps
5082 SecondaryNameNode
5.2
[root@hadp-master sbin]# pwd
/usr/local/spark-2.2.0-bin-hadoop2.7/sbin
[root@hadp-master sbin]# ./start-all.sh
starting org.apache.spark.deploy.master.Master, logging to /usr/local/spark-2.2.0-bin-hadoop2.7/logs/spark-root-org.apache.spark.deploy.master.Master-1-hadp-master.out
hadp-node1: starting org.apache.spark.deploy.worker.Worker, logging to /usr/local/spark-2.2.0-bin-hadoop2.7/logs/spark-root-org.apache.spark.deploy.worker.Worker-1-hadp-node1.out
hadp-node2: starting org.apache.spark.deploy.worker.Worker, logging to /usr/local/spark-2.2.0-bin-hadoop2.7/logs/spark-root-org.apache.spark.deploy.worker.Worker-1-hadp-node2.out
[root@hadp-master sbin]# jps
4914 NameNode
5301 Master
5369 Jps
5082 SecondaryNameNode
[root@hadp-node1 scala-2.11.7]# jps
4305 DataNode
4451 Worker
4500 Jps
[root@hadp-node2 ~]# jps
4444 Worker
4301 DataNode
4494 Jps
— 进入Spark的WebUI界面
http://10.100.25.30:8080/
— 进入 Spark-shell
[root@hadp-master sbin]# spark-shell
Welcome to version 2.2.0
Using Scala version 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_131)
Type in expressions to have them evaluated.
Type :help for more information.
scala>
文本文件中随意输入一些单词,用空格隔开,我们会编写Spark程序对该文件进行单词词频统计。
[root@hadp-master ~]# cat workCount.txt
andy leaf
andy taozi
andy leaf
andy hello
[root@hadp-master ~]# hadoop fs -put workCount.txt /tmp
[root@hadp-master ~]# hadoop fs -ls /tmp
Found 3 items
drwx—— – root supergroup 0 2018-02-01 05:48 /tmp/hadoop-yarn
drwx-wx-wx – root supergroup 0 2018-02-25 05:08 /tmp/hive
-rw-r–r– 1 root supergroup 42 2018-02-25 06:05 /tmp/workCount.txt
[root@hadp-master ~]# hadoop fs -cat /tmp/workCount.txt
andy leaf
andy taozi
andy leaf
andy hello
词频统计
scala> val textFile = sc.textFile("hdfs://hadp-master:9000/tmp/workCount.txt")
scala> val wordCount = textFile.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey((a, b) => a + b)
scala> wordCount.collect()
res0: Array[(String, Int)] = Array((leaf,2), (andy,4), (hello,1), (taozi,1))
上面只给出了代码,省略了执行过程中返回的结果信息,因为返回信息很多。
下面简单解释一下上面的语句。
textFile包含了多行文本内容,textFile.flatMap(line => line.split(” “))会遍历textFile中的每行文本内容,当遍历到其中一行文本内容时,会把文本内容赋值给变量line,并执行Lamda表达式line => line.split(” “)。line => line.split(” “)是一个Lamda表达式,左边表示输入参数,右边表示函数里面执行的处理逻辑,这里执行line.split(” “),也就是针对line中的一行文本内容,采用空格作为分隔符进行单词切分,从一行文本切分得到很多个单词构成的单词集合。这样,对于textFile中的每行文本,都会使用Lamda表达式得到一个单词集合,最终,多行文本,就得到多个单词集合。textFile.flatMap()操作就把这多个单词集合“拍扁”得到一个大的单词集合。
然后,针对这个大的单词集合,执行map()操作,也就是map(word => (word, 1)),这个map操作会遍历这个集合中的每个单词,当遍历到其中一个单词时,就把当前这个单词赋值给变量word,并执行Lamda表达式word => (word, 1),这个Lamda表达式的含义是,word作为函数的输入参数,然后,执行函数处理逻辑,这里会执行(word, 1),也就是针对输入的word,构建得到一个tuple,形式为(word,1),key是word,value是1(表示该单词出现1次)。
程序执行到这里,已经得到一个RDD,这个RDD的每个元素是(key,value)形式的tuple。最后,针对这个RDD,执行reduceByKey((a, b) => a + b)操作,这个操作会把所有RDD元素按照key进行分组,然后使用给定的函数(这里就是Lamda表达式:(a, b) => a + b),对具有相同的key的多个value进行reduce操作,返回reduce后的(key,value),比如(“hadoop”,1)和(“hadoop”,1),具有相同的key,进行reduce以后就得到(“hadoop”,2),这样就计算得到了这个单词的词频。
上述内容就是如何进行spark 2.2.0 Standalone安装及wordCount演示,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
原创文章,作者:506227337,如若转载,请注明出处:https://blog.ytso.com/220501.html