Guest User

Untitled

a guest
Sep 25th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. import org.apache.spark.{SparkConf, SparkContext}
  2. import org.apache.log4j.Logger
  3. import org.apache.log4j.Level
  4.  
  5. object TestScala {
  6. def main(args: Array[String]): Unit = {Logger.getLogger("org").setLevel(Level.OFF)
  7. Logger.getLogger("akka").setLevel(Level.OFF)
  8. print ("start")
  9. val conf = new SparkConf()
  10. conf.setAppName("WordFrequency")
  11. conf.setMaster("local[2]")
  12. // Create a Scala Spark Context.
  13. val sc = new SparkContext(conf)
  14. val inputFile = "product.csv.txt"
  15.  
  16. // Load our input data.
  17. val input = sc.textFile(inputFile)
  18. // Split up into words.
  19.  
  20. val words = input.flatMap(s => s.split(" "))
  21. // words.foreach(print)
  22.  
  23. // print ("proverka")
  24.  
  25. val wordsNumbers = words.map(s => (s, 1))
  26. // Transform into word and count.
  27. val counts = wordsNumbers.reduceByKey ((a, b) => a + b )
  28. // Save the word count back out to a text file, causing evaluation.
  29. val fullCount = words.count().toFloat
  30.  
  31. val wordFrq = counts.map(s => (s._1, s._2, s._2.toFloat / fullCount))
  32. wordFrq.foreach(println)
  33. }
  34. }
Add Comment
Please, Sign In to add comment