Created
March 15, 2019 14:00
-
-
Save corneliouzbett/13d3e88d599b11e3f13220210363f2c4 to your computer and use it in GitHub Desktop.
It is a simple python code using apache spark to count words in text file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyspark import SparkContext, SparkConf | |
def display_words(words): | |
for w, we in words.items(): | |
print("{} : {}".format(w, we)) | |
if __name__ == "__main__": | |
conf = SparkConf().setAppName("word count").setMaster("local[2]") | |
sc = SparkContext(conf = conf) | |
lines = sc.textFile("in/word_count.text") | |
total_lengths = lines.map(lambda s: len(s)).reduce(lambda a,b: a+b) | |
words = lines.flatMap(lambda line: line.split(" ")) | |
wordCounts = words.countByValue() | |
display_words(wordCounts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment