2013-09-28 39 views
0

我有一個程序,迭代mapper和reducer連續的n次。但是,對於每次迭代,每個鍵 - 值對的映射器都會計算出一個取決於n的值。如何使用mrjob mapper reducer在Python中編寫迭代,計數器是循環中計算的一部分?

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類對象,並且該變量不能共享。有沒有另外一種方法來完成這個?

+0

根據我的經驗,這些各種各樣的問題,從您的問題無法正常轉換到MR模式出現。你能提供一些你正在實施的算法的更多細節嗎?我的方法是發出計數*本身*並將其收集在減速器中。請記住,您正在分佈式計算環境中工作 - 無法保證數據在哪裏。 – pcoving

回答

1

你爲什麼不嘗試在課堂上定義你的計數?

class MRWord(MRJob): 
    count = [] 

,丟棄

def mapper_init_def(self): 
    self.count = {}