jlo86 发表于 2016-1-20 09:08:12

用python实现mapreduce

map
# cat mapper.py
#!/usr/bin/env python
import sys
for line in sys.stdin:
      line =line.strip()
      words =line.split()
      for word in words:
                print '%s\t%s' % (word,1)
#

reduce
# vim reducer.py

for line in sys.stdin:
      line = line.strip()
      word,count = line.split('\t',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)

页: [1]
查看完整版本: 用python实现mapreduce