Created
May 11, 2015 15:01
-
-
Save mkemp/3dc2b3a665aa96a2ca69 to your computer and use it in GitHub Desktop.
Used in CloudCamp Chicago 2015.05.11 presentation.
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
#!/usr/bin/env python | |
import re | |
import string | |
regex = re.compile('[%s]' % re.escape(string.punctuation)) | |
def word_count(sc, in_file_name, out_file_name): | |
sc.textFile(in_file_name) \ | |
.flatMap(lambda line: [(word, 1) for word in regex.sub(' ', line).strip().lower().split(' ') if word]) \ | |
.reduceByKey(lambda a, b: a + b) \ | |
.sortByKey() \ | |
.map(lambda (word, count): '%s,%s' % (word, count)) \ | |
.saveAsTextFile(out_file_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment