在Python的哪个循环中,reducer是如何在mapper中编写循环计算的呢?

2024-04-25 22:31:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个程序,它可以连续迭代mapper和reducern次。但是,对于每次迭代,每个键值对的映射器都会计算一个依赖于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()

我定义了一个两步映射器reducer,这样第一步定义了一个类变量self.count。程序产生一个错误,AttributeError: 'MRWord' object has no attribute 'count'。似乎每个步骤都定义了一个独立的mrjob类对象,并且该变量不能共享。有没有别的方法可以做到这一点?在


Tags: keyself程序定义initvaluedefcount