环境配置是:hadoop2.7.2 + zookeeper 3.4.5+ spark 2.1.1
说明:
这里的wordcount是使用scala书写,资源调度使用的是yarn。
1.环境搭建
提前搭建好hadoop集群和zookeeper。
2.搭建spark集群配置
使用的是yarn的集群配置。所以要在启动spark之前线启动hdfs和yarn。
3.代码书写:
def main(args: Array[String]): Unit = {
//1.创建SparkConf并设置App名称
val conf = new SparkConf().setAppName("WC")
//2.创建SparkContext,该对象是提交Spark App的入口
val sc = new SparkContext(conf)
//3.使用sc创建RDD并执行相应的transformation和action
sc.textFile(args(0)).flatMap(_.split(" ")).map((_, 1)).reduceByKey(_+_, 1).sortBy(_._2, false).saveAsTextFile(args(1))
//4.关闭连接
sc.stop()
}
xml配置(maven依赖):
<dependencies>
<!--spark-->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
&l