单词统计的思路
1 初始化SparkConf
2 实例SparkContext 与 clustermanger 建立通讯并监控任务(Driver)
3 读取数据 处理数据
4处理完后的数据进行保存
本地运行(IDEA)
def main(args: Array[String]): Unit = {
// 初始化 sparkConf
val conf: SparkConf = new SparkConf().setAppName("wc").setMaster("local[*]")
//实例一个sparkContext
// 没有把 conf 放到 sc 里面去报的异常
// A master URL must be set in your configuration
val sc: SparkContext = new SparkContext(conf)
// 读取数据 处理数据
val textFile: RDD[String] = sc.textFile("F:\\wordCount.txt")
/*
* 线切割
* 给每个元素标记个1 key 1
* 对可以 进行分组
* key1 1,3,4,5,6
* key2 1,3,4,5,6
* 对value 进行计算
* */
val KV: RDD[(String, Int)] = textFile.flatMap(a=>a.split(" ")).map(b=>(b,1)).reduceByKey(_+_)
// 保存数据
KV.collect().foreach(println(_))
}
集群运行(不要启动spark )Spark 任务提交到yarn 是去
打好jar包
把数据上传到hdfs 上
集群代码
def main(args: Array[String]): Unit = {
val conf = new SparkConf().setAppName("sywc")
val sc = new SparkContext(conf)
val date: RDD[String] = sc.textFile(args(0))
val KV: RDD[(String, Int)] = date.flatMap(x => x.split(" ")).map(z => (z, 1)).reduceByKey(_ + _)
KV.saveAsTextFile(args(1))
}
执行命令
/export/servers/spark-2.2.0-bin-2.6.0-cdh5.14.0/bin/**spark-submi**t \
--class com.czxy.Spark02.Sy \
--master **yarn** \
--deploy-mode cluster \
--driver-memory 1g \
--executor-memory 1g \
--executor-cores 2 \
--queue default \
/opt/yarn.jar \
hdfs://node01:8020/words.txt \
hdfs://node01:8020/yarnWC
Spark 全部的pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<