-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapReduce.py
33 lines (27 loc) · 898 Bytes
/
MapReduce.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import json
class MapReduce:
def __init__(self):
self.intermediate = {}
self.result = []
def emit_intermediate(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):
# print(data)
record = json.loads(data)
print("record")
print(record)
for k, v in record.items():
mapper(k,v)
for key in self.intermediate:
reducer(key, self.intermediate[key])
#jenc = json.JSONEncoder(encoding='latin-1')
jenc = json.JSONEncoder()
banana = {}
for item in self.result:
print(jenc.encode(item))
#romeo = {item[0] : item[1]}
#banana.update(romeo)
#print json.dumps(banana)