Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. from pyspark import SparkContext, SparkConf
  2.  
  3. def display_words(words):
  4. for w, we in words.items():
  5. print("{} : {}".format(w, we))
  6.  
  7. if __name__ == "__main__":
  8. conf = SparkConf().setAppName("word count").setMaster("local[2]")
  9. sc = SparkContext(conf = conf)
  10.  
  11. lines = sc.textFile("in/word_count.text")
  12.  
  13. total_lengths = lines.map(lambda s: len(s)).reduce(lambda a,b: a+b)
  14.  
  15. words = lines.flatMap(lambda line: line.split(" "))
  16.  
  17. wordCounts = words.countByValue()
  18.  
  19. display_words(wordCounts)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement