在使用mrjob的mapreduce作业中使用sort\u值时出现问题(在reducer输入中没有对键值进行排序)

bvjveswy  于 2021-05-29  发布在  Hadoop
关注(0)|答案(0)|浏览(382)

我想创建一个mapreduce程序,它的reduce接收按值排序的k-v对。我使用的是mrjob,它的sort\u values参数似乎非常适合这个任务。将此参数设置为true后,reducer输入没有排序,例如,我得到以下结果(考虑到a应该在x之前):

"ES"    ["X", 3]
"ES"    ["A", "Spain"]

我使用的是python2.7.5、mrjob==0.6.1和hadoop。程序的本地执行给了我:

"ES"    ["A", "Spain"]
"ES"    ["X", 1]
"ES"    ["X", 2]

这是正确的。但是hadoop的执行提供了:

"ES"    ["X", 3]
"ES"    ["A", "Spain"]

我的代码是:

import sys, os, re
from mrjob.job import MRJob
from mrjob.step import MRStep 

class MRJoin(MRJob):

    SORT_VALUES = True

    def mapper(self, _, line):
        splits = line.rstrip("\n").split(",")
        if len(splits) == 2: # countries
            symbol = 'A' # countries before clients
            country2digit = splits[1]
            yield country2digit, (symbol, splits[0])
        else: #  clients
            symbol = 'X'
            country2digit = splits[2]
            if splits[1]=='bueno':
                yield country2digit,(symbol, 1)

    def combiner(self,key, values):
        bueno=0
        for value in values:
            if value[0] == 'A':
                yield key, ('A', value[1])
            else:
                bueno=bueno + 1

        if bueno > 0:
            yield key, ('X', bueno)

    def reducerSimple(self, key, values):
        for value in values:
            yield key,value

    def steps(self): 
        return [ 
            MRStep(mapper=self.mapper 
                   ,combiner=self.combiner
                   ,reducer=self.reducerSimple) 
        ] 

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

我像这样运行上面的代码:
python mrjob-p2.py/media/notebooks/clients.csv/media/notebooks/countries.csv-r hadoop
它给出:

"ES"    ["X", 3]
"ES"    ["A", "Spain"]
...
"GN"    ["A", "Guinea"]
"GN"    ["X", 1]
...

es键(和其他少数键)的值不会被排序,但对于其他键,它们会被排序。
我期望(如果对值进行了排序,那么a应该在x之前):

"ES"    ["A", "Spain"]
"ES"    ["X", 3]

如果我在本地运行:
python mrjob-p2.py/media/notebooks/clients.csv/media/notebooks/countries.csv-r local
然后我得到:

"ES"    ["A", "Spain"]
"ES"    ["X", 1]
"ES"    ["X", 2]
...
"GN"    ["A", "Guinea"]
"GN"    ["X", 1]
...

这是正确的。
有人知道如何把这些值排序吗?
谢谢:)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题