We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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 item
mapReducer = MapReduce()
def mapper(record):
#Start writing the Map code here
fields = record.replace('\n', '').split(',')
def reducer(key, list_of_values):
#Start writing the Reduce code here
person_names = []
department_names = []
for label, value in list_of_values:
if label == 'Employee':
person_names.append(value)
else:
department_names.append(value)
for person_name in person_names:
for department_name in department_names:
mapReducer.emit((key, person_name, department_name))
if name == 'main':
inputData = []
for line in sys.stdin:
inputData.append(line)
mapReducer.execute(inputData, mapper, reducer)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Map Reduce Advanced - Relational Join
You are viewing a single comment's thread. Return to all comments →
from collections import OrderedDict import sys
class MapReduce: def init(self): self.intermediate = OrderedDict() self.result = []
mapReducer = MapReduce()
def mapper(record): #Start writing the Map code here fields = record.replace('\n', '').split(',')
def reducer(key, list_of_values): #Start writing the Reduce code here person_names = [] department_names = [] for label, value in list_of_values: if label == 'Employee': person_names.append(value) else: department_names.append(value)
if name == 'main': inputData = [] for line in sys.stdin: inputData.append(line) mapReducer.execute(inputData, mapper, reducer)