Mapper program : temperature_mapper.py
#!/usr/bin/env python
import sys
# input comes from standard input STDIN
year=0;
for data in sys.stdin:
data = data.strip() #remove leading and trailing whitespaces
yearData = data.split(',') #split the line into words and returns as a list
year = yearData[0];
temperature_data=yearData[1:13]
print('%s %s' % (year, temperature_data)) #write the results to standard output STDOUT
Reducer program: temperature_reducer.py
#!/usr/bin/env python
from operator import itemgetter
import sys
import ast
# input comes from STDIN
for line in sys.stdin:
line = line.strip()
new_year, new_temperature = line.split(' ',1)
list = ast.literal_eval(new_temperature) #Convert string to list
try:
new_year = int(new_year)
except ValueError:
continue
print("%s\t%s"%(new_year,max(list)))
Reducer program : "reducer.py"
#!/usr/bin/env python
from operator import itemgetter
import sys
# using a dictionary to map words to their counts
current_word = None
current_count = 0
word = None
# input comes from STDIN
for line in sys.stdin:
line = line.strip()
word, count = line.split(' ',1)
try:
count = int(count)
except ValueError:
continue
if current_word == word:
current_count += count
else:
if current_word:
print ('%s\t%s'%(current_word, current_count))
current_count = count
current_word = word
if current_word == word:
print ('%s\t%s'%(current_word,current_count))
Mapper program
mapper.py
#!/usr/bin/env python
import sys
#Word Count Example
# input comes from standard input STDIN
for data in sys.stdin:
data = data.strip() #remove leading and trailing whitespaces
lines = data.split() #split the line into words and returns as a list
for word in lines:
#write the results to standard output STDOUT
print('%s %s' % (word, 1))
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}