1.启动一个spark shell
./spark-shell --master spark://hdp-1:7077 --executor-memory 500m --total-executor-cores 1
2.启动在yarn上的spark shell
./spark-shell --master yarn --deploy-mode client
执行时会出现一个HADOOP_HOME未定义的错误,修改方法在spark的conf目录中修改spark-env.sh在最下边加入
export HADOOP_CONF_DIR=$HADOOP_HOME/etc/hadoop 即可修复问题
再次执行启动代码会出现一个yarn提前kill进程的错误
修改方法:在/root/apps/hadoop-2.8.1/etc/hadoop中修改yarn-site.xml文件,加入
<property>
<name>yarn.nodemanager.vmem-check-enabled</name>
<value>false</value>
<description>Whether virtual memory limits will be enforced for containers</description>
</property>
<property>
<name>yarn.nodemanager.vmem-pmem-ratio</name>
<value>4</value>
<description>Ratio between virtual memory to physical memory when setting memory limits for containers</description>
</property>
即可修复。
3.在hdfs中创建一个存储数据的位置/spark/input/hello.txt 数据为
Hello you
Hello me
在spark shell 或者 yarn的spark shell中执行命令
sc.textFile("hdfs://hdp-1:9000/spark/input/hello.txt").flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_).saveAsTextFile("hdfs://hdp-1:9000/spark/output")
便会执行一个wordcount程序,并将最总处理后的结果存储到hdfs://hdp-1:9000/spark/output中
在IDEA中写一个Word Count
1.创建一个maven工厂,在pom文件中加入依赖,由于个人的spark安装的为2.4.4版本所以依赖注入的版本也相同
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<scala.version>2.11.8</scala.version>
<spark.version>2.4.4</spark.version>
<hadoop.version>2.6.5</hadoop.version>
<encoding>UTF-8</encoding>
</properties>
<dependencies>
<!-- 导入scala的依赖 -->
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<!-- 导入spark的依赖 -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>${spark.version}</version>
</dependency>
<!-- 指定hadoop-client API的版本 -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<!-- 编译scala的插件 -->
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<!-- 编译java的插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 打jar插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
2.创建一个scala资源文件夹目录结构为
3.在WordCount中编写程序
package com.zpark.stu
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}
object WoedCount {
def main(args: Array[String]): Unit = {
//创建spark配置,设置应用程序名字
//val conf = new SparkConf().setAppName("ScalaWordCount")
val conf = new SparkConf().setAppName("ScalaWordCount").setMaster("local[4]")
//创建spark执行的入口
val sc = new SparkContext(conf)
//指定以后从哪里读取数据创建RDD(弹性分布式数据集)
//sc.textFile(args(0)).flatMap(_.split(" ")).map((_, 1)).reduceByKey(_+_).sortBy(_._2, false).saveAsTextFile(args(1))
val lines: RDD[String] = sc.textFile(args(0))
//切分压平 _代表一行内容
val words: RDD[String] = lines.flatMap(_.split(","))
//将单词和一组合,形成一个元组
val wordAndOne: RDD[(String, Int)] = words.map((_, 1))
//按key进行聚合
val reduced:RDD[(String, Int)] = wordAndOne.reduceByKey(_+_)
//排序
val sorted: RDD[(String, Int)] = reduced.sortBy(_._2, false)
//将结果保存到HDFS中
sorted.saveAsTextFile(args(1))
//释放资源
sc.stop()
}
}
4.将程序打成jar包上传到虚拟机中执行
./spark-submit --master spark://hdp-1:7077 --class com.zpark.stu.WoedCount /root/original-sparkwordcount-1.0-SNAPSHOT.jar hdfs://hdp-1:9000/spark/input/hello.txt hdfs://hdp-1:9000/spark/out