Running a Spark Word Count in IntelliJ

前端 未结 3 1848
失恋的感觉
失恋的感觉 2021-01-26 08:03

I\'ve spent hours going through You Tube vids and tutorials trying to understand how I run a run a word count program for Spark, in Scala, and the turn it into a jar file. I\'m

3条回答
  •  忘掉有多难
    2021-01-26 08:26

    check the sample code which i written show below

    package com.spark.app
    
    import org.scalatra._
    import org.apache.spark.{ SparkContext, SparkConf }
    
    class MySparkAppServlet extends MySparkAppStack {
    
      get("/wc") {
            val inputFile = "/home/limitless/Documents/projects/test/my-spark-app/README.md"
            val outputFile = "/home/limitless/Documents/projects/test/my-spark-app/README.txt"
            val conf = new SparkConf().setAppName("wordCount").setMaster("local[*]")
            val sc = new SparkContext(conf)
            val input =  sc.textFile(inputFile)
            val words = input.flatMap(line => line.split(" "))
            val counts = words.map(word => (word, 1)).reduceByKey{case (x, y) => x + y}
            counts.saveAsTextFile(outputFile)
        }
    
    }
    

提交回复
热议问题