Ambari2.7.3-Spark2.3.2 提交wordcount任务

0.准备

在这里插入图片描述

1.数据准备

hello world
dog fish
hadoop
spark
hello world
dog fish
hadoop
spark
hello world
dog fish
hadoop
spark

在这里插入图片描述

2.执行命令

sc.textFile("hdfs://managerhd.bigdata:8020/wordcount").flatMap(_.split(" ")).map((_, 1)).reduceByKey(_+_).sortBy(_._2, false).collect
在这里插入图片描述

sc.textFile("hdfs://managerhd.bigdata:8020/wordcount").flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_).sortBy(_._2, false).saveAsTextFile("hdfs://managerhd.bigdata:8020/wordcountoutput")
在这里插入图片描述
在这里插入图片描述

选项 说明
sc SparkContext对象,该对象是提交spark的程序入口
testFile() hdfs中读取数据
flatMap(_.split(" ")) 拆分数据
map((_,1)) 将单词和 1 构成元组
reduceByKey(_+_) 按照key进行reduce,并将value累加
saveAsTextFile() 将结果写入hdfs中
发布了153 篇原创文章 · 获赞 122 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/Happy_Sunshine_Boy/article/details/104988590