实验内容:安装配置Spark以及Scala语言环境,分别使用Spark语言和Scala语言编写 WordCount程序
实验步骤:
一 .下载scalar
解压到路径/usr/local/scalar
在/etc/profile文件中加入安装路径
vim /etc/profile
添加以下内容
export SCALA_HOME=/usr/local/scala/scala-2.12.8
export PATH=$PATH:$SCALA_HOME/bin
执行文件source /etc/profile
安装完成,验证是否成功:scala -version
出现以下的说明Scala安装成功
Scala code runner version 2.12.8 -- Copyright 2002-2018, LAMP/EPFL and Lightbend, Inc.
要在每一个节点上都安装配置scalar,安装完后发送给slave1、slave2
scp -r /usr/local/scala/scala-2.12.8 root @slave1:/usr/local/scala
scp -r /usr/local/scala/scala-2.12.8 root @slave2:/usr/local/scala
二 .下载spark安装
- 下载spark:
http://mirrors.tuna.tsinghua.edu.cn/apache/spark/spark-2.4.2/spark-2.4.2-bin-hadoop2.7.tgz
- 安装解压到以下路径/usr/local/spark
编辑/etc/profile文件,增加:
export SPARK_HOME=/usr/local/spark/spark-2.4.2-bin-hadoop2.7
export PATH=$PATH:$SPARK_HOME/bin
- 生效:source /etc/profile
- 修改:spark-env.sh
- 进入spark安装目录操作(spark)
cp conf/spark-env.sh.template conf/spark-env.sh
vi conf/spark-env.sh
OME=/opt/module/jdk1.8.0_121
export SCALA_HOME=/usr/local/scala
export HADOOP_HOME=/opt/module/hadoop-2.7.3/
export SPARK_MASTER_WEBUI_PORT=8080
export SPARK_MASTER_HOST=spark-master
export SPARK_MASTER_PORT=7077
export SPARK_MASTER_IP=spark-master
export SPARK_WORKER_WEBUI_PORT=8080
export SPARK_WORKER_MEMORY=1g
- 新建slaves文件:
cp slaves.template slaves
- 编辑slaves文件
里面的内容删除,修改为:
slaver1
slaver2
配置完成,分发给其他节点,并且完成/etc/profile文件的配置
scp -r /usr/local/spark/spark-2.4.2 root @slave1:/usr/local/spark
scp -r /usr/local/spark/spark-2.4.2 root @slave2:/usr/local/spark
- 启动spark
sbin/start-all.sh
使用spark-shell
执行 spark-shell
scala>val textFile = sc.textFile("file:///opt/module/code/wordcount/word.txt")
scala>textFile.first()
val textFile = sc.textFile("hdfs://bigdata128:9000/user/root/word.txt")
textFile.first()
val wordCount = textFile.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey((a, b) => a + b)
wordCount.collect()
- 安装sbt打包:
curl https://bintray.com/sbt/rpm/rpm > bintray-sbt-rpm.repo
mv bintray-sbt-rpm.repo /etc/yum.repos.d/
yum install sbt
- 运行sbt:
安装sbt成功:CTRL+C退出sbt
回到root目录,创建一个文件夹test
创建WordCount.scala:
创建word-count.sbt:
执行打包
打开test02, part-00000文件即是程序的输出文件。