簡體   English   中英

MRJob分揀減速器輸出

[英]MRJob sort reducer output

有什么辦法可以使用mrjob對reducer函數的輸出進行排序?

我認為減速器功能的輸入是按鍵排序的,我試圖利用此功能使用另一個約化器對輸出進行排序,如下所示,我知道值具有數字值,我想對每個鍵的數量進行計數並根據此計數:

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)

好吧,這是對內存中的輸出進行排序,這可能會成為問題,具體取決於輸入的大小。 但是您希望對其進行排序,因此必須以某種方式對其進行排序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM