分类目录

链接

2022 年 5 月
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > 大数据 > 正文
spark版hello word
大数据 暂无评论 阅读(77)
  1. import org.apache.spark.SparkContext
  2. import org.apache.spark.SparkContext._
  3. import org.apache.spark.SparkConf
  4.  
  5. object WordCount {
  6.   def main(args: Array[String]) {
  7.     val inputFile =  "/Users/artefact/software/spark-3.1.3-bin-hadoop3.2/data/wordcount.txt"
  8.     val conf = new SparkConf().setAppName("WordCount").setMaster("local")
  9.     val sc = new SparkContext(conf)
  10.     val textFile = sc.textFile(inputFile)
  11.     val wordCount = textFile
  12.       .flatMap(_.split(" ")) //.flatMap(line => line.split(" "))
  13.       .filter(f=> !f.equals(""))
  14.       .map(word => (word, 1))
  15.       .reduceByKey((a, b) => a + b)
  16.       .sortBy(key=>key._1) //_1=ASC, _2=DESC
  17.     wordCount.foreach(println)
  18.   }
  19. }

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:spark版hello word | Bruce's Blog

发表评论

留言无头像?