-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_reduce.py
90 lines (72 loc) · 2.36 KB
/
map_reduce.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import requests
import logging
import master
class MapReduceLibrary:
def __init__(self):
self.initiated = False
self.mr = master.MapReduce()
def init_cluster(
self,
count_mapper,
count_reducer
):
"""
Initiates Map Reduce Cluster
:param count_mapper: integer
:param count_reducer: integer
:return: string
"""
try:
self.initiated = True
response = requests.get(
'http://localhost:8000//init',
params={
"count_mapper": count_mapper,
"count_reducer": count_reducer
},
headers={'content-type': 'application/json'}
)
return response
except Exception as e:
logging.error(str(e))
raise e
def run_mapred(
self,
file_path,
map_func_type,
reduce_func_type,
output_file_path
):
"""
Starts map reduce
:param file_path: string
:param map_func_type: string (word_count, inverted_index)
:param reduce_func_type: string (word_count, inverted_index)
:param output_file_path: string
:return: string
"""
try:
if not self.initiated:
raise Exception("Init cluster not called first")
if map_func_type != reduce_func_type:
raise Exception("Map and Reduce function type should be same")
if map_func_type == reduce_func_type == 'word_count' or\
map_func_type == reduce_func_type == 'inverted_index':
response = requests.get(
'http://localhost:8000/mapreduce',
params={
"file_path": file_path,
"map_func_type": map_func_type,
"reduce_func_type": reduce_func_type,
"output_file_path": output_file_path
},
headers={
'content-type': 'application/json'
}
)
return response
else:
return "Unknown Map Reduce Type", 400
except Exception as e:
logging.error(str(e))
raise e