spark word frequency statistics

Directly on the code: show you the codes

单词个数统计,首先将以行语句切分为每个单词,并标记为1,然后这个单词进行统计累加,最后算出每个单词的个数

3.2 Editing object created above "SparkWordCountTest", and then edit the content as follows

    import org.apache.spark.SparkConf        
    import org.apache.spark.SparkContext        

    object SparkWordCountTest {        

      def main(args: Array[String]) {        
        val conf = new SparkConf().setAppName("WordCount").setMaster("local[4]")        
        val sc = new SparkContext(conf)        

        val lines = sc.textFile("filePath", 1)        
        val words = lines.flatMap { line => line.split(" ") }        
        val pairs = words.map { word => (word, 1) }        
        val wordCounts = pairs.reduceByKey { _ + _ }        

       wordCounts.foreach(wordCount => println(wordCount._1 + "---->" + wordCount._2))        

      }        

    }
Published 10 original articles · won praise 5 · Views 926

Guess you like

Origin blog.csdn.net/qq_42774323/article/details/102667301