You are viewing a single comment's thread. Return to all comments →
import sys from collections import OrderedDict class MapReduce: def __init__(self): self.intermediate = OrderedDict() self.result = [] def emitIntermediate(self, key, value): self.intermediate.setdefault(key, []) self.intermediate[key].append(value) def emit(self, value): self.result.append(value) def execute(self, data, mapper, reducer): for record in data: mapper(record) for key in self.intermediate: reducer(key, self.intermediate[key]) self.result.sort() for item in self.result: print "{\"key\":\""+item[0]+"\",\"value\":\"" + str(item[1]) + "\"}" mapReducer = MapReduce() def mapper(record): people = record.rstrip().split(" ") mapReducer.emitIntermediate(people[0], 1) mapReducer.emitIntermediate(people[1], 1) def reducer(key, list_of_values): total = sum(list_of_values) mapReducer.emit((key, total)) if __name__ == '__main__': inputData = [] for line in sys.stdin: inputData.append(line) mapReducer.execute(inputData, mapper, reducer)
Seems like cookies are disabled on this browser, please enable them to open this website
Map Reduce Advanced - Count number of friends
You are viewing a single comment's thread. Return to all comments →