如何编写用于矩阵加法的MRJob python



我一直在尝试用MRJob库制作简单的矩阵加法程序。我已经创建了这个简单的程序,因为它有一个单独的映射器和reducer,它在本地和Hadoop集群上运行良好,现在我正试图在单个类(MRJob(库上创建这个程序。但我找不到我得到的错误,mapper函数工作正常,但reducer工作不正常,我在互联网上找不到任何解决方案。如有任何建议,我们将不胜感激。

这是输入文件(input.txt(:

a,0,0,10
a,0,1,20
a,0,2,30
a,1,0,40
a,1,1,50
a,1,2,60
a,2,0,70
a,2,1,80
a,2,2,90
b,0,0,1
b,0,1,2
b,0,2,3
b,1,0,4
b,1,1,5
b,1,2,6
b,2,0,7
b,2,1,8
b,2,2,9

这是代码文件:

from mrjob.job import MRJob

class MRWordFrequencyCount(MRJob):
def mapper(self, _, line):
row = line.strip().split(',')
key = row[0]
value = row[1:]
if key == 'a':
yield key, value
elif key == 'b':
yield key, value
def reducer(self, key, value):
A = dict()
B = dict()
if key == 'a':
A[int(value[0]), int(value[1])] = int(value[2])
elif key == 'b':
B[int(value[0]), int(value[1])] = int(value[2])
for row in range(3):
for col in range(3):
yield (row, col), A[(row, col)]+B[(row, col)]

if __name__ == '__main__':
MRWordFrequencyCount.run()

错误我正在获取

No configs found; falling back on auto-configuration
No configs specified for inline runner
Creating temp directory /tmp/MRJMatrix.ahsan.20220228.212900.538764
Running step 1 of 1...
Error while reading from /tmp/MRJMatrix.ahsan.20220228.212900.538764/step/000/reducer/00000/input:
Traceback (most recent call last):
File "MRJMatrix.py", line 29, in <module>
MRWordFrequencyCount.run()
File "/home/ahsan/.local/lib/python3.8/site-packages/mrjob/job.py", line 616, in run
cls().execute()
File "/home/ahsan/.local/lib/python3.8/site-packages/mrjob/job.py", line 687, in execute
self.run_job()
File "/home/ahsan/.local/lib/python3.8/site-packages/mrjob/job.py", line 636, in run_job
runner.run()
File "/home/ahsan/.local/lib/python3.8/site-packages/mrjob/runner.py", line 503, in run
self._run()
File "/home/ahsan/.local/lib/python3.8/site-packages/mrjob/sim.py", line 161, in _run
self._run_step(step, step_num)
File "/home/ahsan/.local/lib/python3.8/site-packages/mrjob/sim.py", line 170, in _run_step
self._run_streaming_step(step, step_num)
File "/home/ahsan/.local/lib/python3.8/site-packages/mrjob/sim.py", line 187, in _run_streaming_step
self._run_reducers(step_num, num_reducer_tasks)
File "/home/ahsan/.local/lib/python3.8/site-packages/mrjob/sim.py", line 289, in _run_reducers
self._run_multiple(
File "/home/ahsan/.local/lib/python3.8/site-packages/mrjob/sim.py", line 130, in _run_multiple
func()
File "/home/ahsan/.local/lib/python3.8/site-packages/mrjob/sim.py", line 746, in _run_task
invoke_task(
File "/home/ahsan/.local/lib/python3.8/site-packages/mrjob/inline.py", line 133, in invoke_task
task.execute()
File "/home/ahsan/.local/lib/python3.8/site-packages/mrjob/job.py", line 681, in execute
self.run_reducer(self.options.step_num)
File "/home/ahsan/.local/lib/python3.8/site-packages/mrjob/job.py", line 795, in run_reducer
for k, v in self.reduce_pairs(read_lines(), step_num=step_num):
File "/home/ahsan/.local/lib/python3.8/site-packages/mrjob/job.py", line 866, in reduce_pairs
for k, v in self._combine_or_reduce_pairs(pairs, 'reducer', step_num):
File "/home/ahsan/.local/lib/python3.8/site-packages/mrjob/job.py", line 889, in _combine_or_reduce_pairs
for k, v in task(key, values) or ():
File "MRJMatrix.py", line 19, in reducer
A[int(value[0]), int(value[1])] = int(value[2])
TypeError: 'generator' object is not subscriptable

减速器的输入如下:(a,[[values1]],[[values]]….(所以,现在值是一个生成器,它是可迭代的,但您不能访问它,因为它是一个列表。您可以方便地转换列表中的值,并通过for循环在列表中滚动。作为参考,你也可以在这里看到:';发电机';对象不可下标";错误

我希望它有用:(

欢呼,

相关内容

  • 没有找到相关文章

最新更新