Repository navigation
Expand file tree
/
Copy pathmultiply.py
More file actions
54 lines (42 loc) · 1.34 KB
/
Copy pathmultiply.py
File metadata and controls
54 lines (42 loc) · 1.34 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
45
46
47
48
49
50
51
52
53
54
__author__ = 'Mehmet Ahat'
import MapReduce
import sys
mr = MapReduce.MapReduce()
# =============================
# Do not modify above this line
num_cols = 5
num_rows = 5
def mapper(record):
# key: index of matrix which will be result of multiplication of a and b
# string are user for key values in order to simplify the logic
# value: records
mat = record[0]
i = record[1]
j = record[2]
if mat == 'a':
for x in range(0, num_cols):
key = str(i) + ',' + str(x)
mr.emit_intermediate(key, record)
if mat == 'b':
for x in range(0, num_rows):
key = str(x) + ',' + str(j)
mr.emit_intermediate(key, record)
def reducer(key, list_of_values):
# key: index of result matrix
# value: multiplication result
a_list = [x for x in list_of_values if x[0] == 'a']
b_list = [x for x in list_of_values if x[0] == 'b']
total = 0
for a in a_list:
for b in b_list:
if a[2] == b[1]:
total += a[3] * b[3]
key_in = key.split(',')
mr.emit((int(key_in[0]), int(key_in[1]), total))
# Do not modify below this line
# =============================
if __name__ == '__main__':
# filePath = ".//data//matrix.json"
# inputdata = open(filePath)
inputdata = open(sys.argv[1])
mr.execute(inputdata, mapper, reducer)