-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimple_NewLine_Counter.py
More file actions
44 lines (32 loc) · 1.18 KB
/
Copy pathSimple_NewLine_Counter.py
File metadata and controls
44 lines (32 loc) · 1.18 KB
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
from CustomMapReduce.InputData import InputData
from CustomMapReduce.PathInputData import PathInputData
from CustomMapReduce.Worker import Worker
import os
from threading import Thread
class LineCountWorker(Worker):
def map(self):
data = self.input_data.read()
self.result = data.count('\n')
def reduce(self, other):
self.result += other.result
def generate_inputs(data_dir):
for name in os.listdir(data_dir):
yield PathInputData(os.path.join(data_dir,name))
def create_workers(input_list):
workers = []
for input_data in input_list:
workers.append(LineCountWorker(input_data))
return workers
def execute(workers):
threads = [Thread(target=w.map) for w in workers]
for thread in threads: thread.start()
for thread in threads: thread.join()
first, rest = workers[0], workers[1:]
for worker in rest:
first.reduce(worker)
return first.result
def mapReduce(data_dir):
inputs = generate_inputs(data_dir)
workers = create_workers(inputs)
return execute(workers)
print(mapReduce('/home/bharat/Code_Repositories/Python/Python/CustomMapReduce/data'))