我有一个程序可以n
连续迭代映射器和减速器时间。但是,对于每次迭代,每个键值对的映射器都会计算一个取决于 的值n
。
from mrjob.job import mrjob
class MRWord(mrjob):
def mapper_init_def(self):
self.count = {}
def mapper_count(self, key, value):
self.count[key] = 0
print self.count[key]
# print correctly
yield key, value
def mapper_iterate(self, key, value):
yield key, value
print self.count[key]
#error
def reducer_iterate(self, key, value):
yield key, value
def steps(self):
return [
self.mr(mapper_init=self.mapper_init_def, mapper=self.mapper_count),
self.mr(mapper=self.mapper_iterate, reducer=self.reducer_iterate)
]
if __name__ == '__main__':
MRWord.run()
我定义了一个两步映射器减速器,这样第一个定义了一个类变量,self.count
. 程序产生错误,AttributeError: 'MRWord' object has no attribute 'count'
. 似乎每个步骤都定义了一个独立的 mrjob 类对象,并且该变量不能共享。还有另一种方法可以做到这一点吗?