spark实现wordCount

object wordCount extends App {
  val conf = new SparkConf().setAppName("spark").setMaster("local[3]")
  val sc = new SparkContext(conf)
  //按行读取
  val lines : RDD[String] = sc.textFile("data/word.txt")
  lines.foreach( x => {println(x)})
  //val word : RDD[String] = lines.filter(x => {x.contains("a")})
 val word : RDD[String] = lines.flatMap(x => {x.split(" ")})
  word.foreach(x => {println(x)})
 val result = word.map(x => {(x, 1)}).reduceByKey(_+_).foreach(println)

}

你可能感兴趣的:(spark实现wordCount)