Python MRJob排序减速机输出
是否有任何方法可以使用mrjob对减速器功能的输出进行排序 我认为reducer函数的输入是按键排序的,我尝试利用此功能使用另一个reducer对输出进行排序,如下所示,我知道值有数值,我想计算每个键的数量,并根据此计数对键进行排序:Python MRJob排序减速机输出,python,sorting,mapreduce,mrjob,Python,Sorting,Mapreduce,Mrjob,是否有任何方法可以使用mrjob对减速器功能的输出进行排序 我认为reducer函数的输入是按键排序的,我尝试利用此功能使用另一个reducer对输出进行排序,如下所示,我知道值有数值,我想计算每个键的数量,并根据此计数对键进行排序: def mapper_1(self, key, line): key = #extract key from the line yield (key, 1) def reducer_1(self, key, values): yield
def mapper_1(self, key, line):
key = #extract key from the line
yield (key, 1)
def reducer_1(self, key, values):
yield key, sum(values)
def mapper_2(self, key, count):
yield ('%020d' % int(count), key)
def reducer_2(self, count, keys):
for key in keys:
yield key, int(count)
但它的输出没有正确排序!我怀疑这种奇怪的行为是由于将int
s作为string
进行操作造成的,并试图按照所说的方式对其进行格式化,但没有成功
重要提示:当我使用调试器查看reducer_2
的输出顺序时,顺序是正确的,但打印为输出的是其他内容
重要提示2:在另一台计算机上,相同数据上的相同程序返回按预期排序的输出 您可以在第二个reducer中将值排序为整数,然后将其转换为零填充表示法:
import re
from mrjob.job import MRJob
from mrjob.step import MRStep
WORD_RE = re.compile(r"[\w']+")
class MRWordFrequencyCount(MRJob):
def steps(self):
return [
MRStep(
mapper=self.mapper_extract_words, combiner=self.combine_word_counts,
reducer=self.reducer_sum_word_counts
),
MRStep(
reducer=self.reduce_sort_counts
)
]
def mapper_extract_words(self, _, line):
for word in WORD_RE.findall(line):
yield word.lower(), 1
def combine_word_counts(self, word, counts):
yield word, sum(counts)
def reducer_sum_word_counts(self, key, values):
yield None, (sum(values), key)
def reduce_sort_counts(self, _, word_counts):
for count, key in sorted(word_counts, reverse=True):
yield ('%020d' % int(count), key)
这就是在内存中对输出进行排序,这可能是一个问题,取决于输入的大小。但是你想要它被分类,所以它必须以某种方式被分类 谢谢,成功了!但还有一点,为什么我的代码在不同的机器上产生不同的输出!我的代码对除我之外的所有计算机上的输出进行排序!!!什么是操作系统?MRJob、python等的版本?我使用的是MacOs、python3.5和mrjob0.6.6。它是用anaconda在多个窗口上排序的,我不知道它的版本!还有它正在排序的其他mashine?:)我有Ubuntu16、Python3.6.2和mrjob 0.6.6,它并没有为我排序。您链接的mrjob问题上有一条评论,建议在合并步骤后对其进行排序,但仅适用于小输入,无论小输入意味着什么。