5 Write Mapper.py, Reducer.py and Run in Hadoop - arwankhoiruddin/hadoopLab GitHub Wiki
Create mapper.py
nano mapper.py
This is the file content
#!/usr/bin/env python
"""mapper.py"""
import sys
# input comes from STDIN (standard input)
for line in sys.stdin:
# remove leading and trailing whitespace
line = line.strip()
# split the line into words
words = line.split()
# increase counters
for word in words:
# write the results to STDOUT (standard output);
# what we output here will be the input for the
# Reduce step, i.e. the input for reducer.py
#
# tab-delimited; the trivial word count is 1
print '%s\t%s' % (word, 1)
Create reducer.py
nano reducer.py
This is the code for reducer.py
#!/usr/bin/env python
"""reducer.py"""
from operator import itemgetter
import sys
current_word = None
current_count = 0
word = None
# input comes from STDIN
for line in sys.stdin:
# remove leading and trailing whitespace
line = line.strip()
# parse the input we got from mapper.py
word, count = line.split('\t', 1)
# convert count (currently a string) to int
try:
count = int(count)
except ValueError:
# count was not a number, so silently
# ignore/discard this line
continue
# this IF-switch only works because Hadoop sorts map output
# by key (here: word) before it is passed to the reducer
if current_word == word:
current_count += count
else:
if current_word:
# write result to STDOUT
print '%s\t%s' % (current_word, current_count)
current_count = count
current_word = word
# do not forget to output the last word if needed!
if current_word == word:
print '%s\t%s' % (current_word, current_count)
Make both files executable
chmod +x mapper.py
chmod +x reducer.py
Test the code in local
cat pg20417.txt|./mapper.py|sort|./reducer.py
Run in Hadoop
./bin/hadoop jar ./share/hadoop/tools/lib/hadoop-streaming.jar -file ../data/mapper.py -mapper ../data/mapper.py -file /data/reducer.py -reducer ../data/reducer.py -input /input/pg20417.txt -output /output