我想修改高级教程的内部连接示例,使其可以使用mapreduce进行稀疏矩阵乘法(由Ullman描述)。因此,我需要第二个map-reduce步骤,将结果矩阵中相等位置的值相加。
不幸的是,我没有设法将类CsvInnerJoin的第一个reduce函数的输出转换为SumJob的map函数。
import sys
sys.path.append("/home/damian/disco/lib/")
from disco.core import Job, result_iterator
from disco.worker.classic.func import chain_reader
import csv, sys
if __name__ == '__main__':
input_filename = "input.csv"
output_filename = "output.csv"
if len(sys.argv) > 1:
input_filename = sys.argv[1]
if len(sys.argv) > 2:
output_filename = sys.argv[2]
from CsvInnerJoiner import CsvInnerJoiner
from SumJob import SumJob
job = CsvInnerJoiner().run(input=[input_filename])
job = SumJob().run() (******************)
with open(output_filename, 'w') as fp:
writer = csv.writer(fp)
for url_key, descriptors in result_iterator(job.wait(show=True)):
writer.writerow([url_key] + descriptors)
CsvInnerJoiner.py就是这个文件:
import sys
sys.path.append("/home/damian/disco/lib/")
from disco.core import Job, result_iterator
from disco.worker.classic.func import chain_reader
import csv, sys
class CsvInnerJoiner(Job):
partitions = 2
sort = True
def map(self, row, params):
yield row[0], row[1:]
@staticmethod
def map_reader(fd, size, url, params):
reader = csv.reader(fd, delimiter=',')
for row in reader:
yield row
#@staticmethod
def reduce(self, rows_iter, out, params):
from disco.util import kvgroup
from itertools import chain
#for url_key, descriptors in kvgroup(sorted(rows_iter)):
for url_key, descriptors in kvgroup(rows_iter):
merged_descriptors = list(chain.from_iterable(descriptors))
print url_key,"_______",merged_descriptors
if len(merged_descriptors) > 3:
Alist = merged_descriptors[:merged_descriptors.index("B")]
Blist = merged_descriptors[merged_descriptors.index("B"):]
Alistlength = len(Alist)/3
Blistlength = len(Blist)/3
for i in range(Alistlength):
for j in range(Blistlength):
container = int(Alist[3*i+2])*int(Blist[3*j+2])
yield [Alist[3*i+1],Blist[3*j+1]],container
#out.add(Alist[3*i+1],[Blist[3*j+1],container])
SumJob.py就是这样:
import sys
sys.path.append("/home/damian/disco/lib/")
from disco.core import Job, result_iterator
from disco.worker.classic.func import chain_reader
import csv, sys
class SumJob(Job):
map_reader = staticmethod(chain_reader)
@staticmethod
def map(self,key_value, params):
print "KEY::::::",str(key_value[0])
print "VAL::::::",str(key_value[1])
yield key_value[0], key_value[1]
@staticmethod
def reduce(self,key_value,out, params):
Summe = sum(key_value[1])
out.add(key_value[0],Summe)
问题是我不知道如何更改(**)行,以便第一个reduce步骤的第二个输出被第二个map-function视为输入。
非常感谢你的帮助! 达米安
答案 0 :(得分:0)
您可以使用map / reduce阶段的输出作为另一个的输入(从job.wait()
返回)。
job1 = SumJob().run(input=[...])
job2 = SumJob().run(input=[...])
output = SomeOtherJob.run(input=[job1.wait(), job2.wait()]).wait(show=True)
for key, value in result_iterator(output):
print key, value
我不是那种代码适合我的专家(我实现了pagerank
算法,它有很多阶段和多次迭代)。