Facebook
From Labexp, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 134
  1. Mapper program : temperature_mapper.py
  2. #!/usr/bin/env python
  3. import sys
  4. # input comes from standard input STDIN
  5. year=0;
  6. for data in sys.stdin:
  7.  data = data.strip() #remove leading and trailing whitespaces
  8.  yearData = data.split(',') #split the line into words and returns as a list
  9.  year = yearData[0];
  10.  temperature_data=yearData[1:13]
  11.  print('%s %s' % (year, temperature_data)) #write the results to standard output STDOUT
  12. Reducer program: temperature_reducer.py
  13. #!/usr/bin/env python
  14. from operator import itemgetter
  15. import sys
  16. import ast
  17. # input comes from STDIN
  18. for line in sys.stdin:
  19.  line = line.strip()
  20.  new_year, new_temperature = line.split(' ',1)
  21.  list = ast.literal_eval(new_temperature) #Convert string to list
  22.  try:
  23.  new_year = int(new_year)
  24.  except ValueError:
  25.  continue
  26.  print("%s\t%s"%(new_year,max(list)))
  27.  Reducer program : "reducer.py"
  28. #!/usr/bin/env python
  29. from operator import itemgetter
  30. import sys
  31. # using a dictionary to map words to their counts
  32. current_word = None
  33. current_count = 0
  34. word = None
  35. # input comes from STDIN
  36. for line in sys.stdin:
  37.  line = line.strip()
  38.  word, count = line.split(' ',1)
  39.  try:
  40.  count = int(count)
  41.  except ValueError:
  42.  continue
  43.  if current_word == word:
  44.  current_count += count
  45.  else:
  46.  if current_word:
  47.  print ('%s\t%s'%(current_word, current_count))
  48.  current_count = count
  49.  current_word = word
  50. if current_word == word:
  51.  print ('%s\t%s'%(current_word,current_count))
  52.  
  53.  
  54.  Mapper program
  55. mapper.py
  56. #!/usr/bin/env python
  57. import sys
  58. #Word Count Example
  59. # input comes from standard input STDIN
  60. for data in sys.stdin:
  61.  data = data.strip() #remove leading and trailing whitespaces
  62.  lines = data.split() #split the line into words and returns as a list
  63.  for word in lines:
  64.  #write the results to standard output STDOUT
  65.  print('%s %s' % (word, 1))