1. 程式人生 > >spark scala wordcount 例子

spark scala wordcount 例子

word () lines atm nes flat val string println

object ScalaApp {

  def main(args: Array[String]): Unit = {
    var conf = new SparkConf()
    conf.setMaster("local")
    var sc = new SparkContext(conf)
    val lines: RDD[String] = sc.textFile("./words.txt")
    lines.flatMap(line=>line.split(" ")).map(word=>(word,1)).reduceByKey((v1,v2)=>v1+v2).foreach
(println) // 簡潔版 // lines.flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_).foreach(println) } }

spark scala wordcount 例子