【问题标题】:Python3 MRJob outputs unsorted key-value pairsPython3 MRJob 输出未排序的键值对
【发布时间】:2018-05-09 01:44:08
【问题描述】:

上下文

Python 3.6.3 :: Anaconda 自定义(64 位)
mrjob==0.6.2 没有自定义配置
正在运行本地

我正在为本地地图减少作业实施基本字数统计示例。我的映射器使用一个简单的正则表达式将一个 1 映射到来自 .txt 文件的书的每一行中的每个单词。 reducer 计算每个单词的出现次数,即每个单词分组的 1 的数量。

from mrjob.job import MRJob
import re

WORD_REGEXP = re.compile(r"[\w']+")

class WordCounter(MRJob):
  def mapper(self, _, line):
    words = WORD_REGEXP.findall(line)
    for word in words:
      yield word.lower(), 1

  def reducer(self, word, times_seen):
    yield word, sum(times_seen)

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

问题

输出文件正确,但键值对未全局排序。结果似乎只在数据块中按字母顺序排序。

"customers'"    1
"customizing"   1
"cut"   2
"cycle" 1
"cycles"    1
"d" 10
"dad"   1
"dada"  1
"daily" 3
"damage"    1
"deductible"    6
...
"exchange"  10
"excited"   4
"excitement"    1
"exciting"  4
"executive" 2
"executives"    2
"theft" 1
"their" 122
"them"  166
"theme" 2
"themselves"    16
"then"  59
"there" 144
"they've"   2
...
"anecdotes" 1
"angel" 1
"angie's"   1
"angry" 1
"announce"  2
"announced" 1
"announcement"  3
"announcements" 3
"announcing"    2
...
"patents"   3
"path"  19
"paths" 1
"patterns"  1
"pay"   45
"exercise"  1
"exercises" 1
"exist" 6
"expansion" 1
"expect"    11
"expectation"   3
"expectations"  5
"expected"  4
....
"customer"  41
"customers" 122
"yours" 15
"yourself"  78
"youth" 1
"zealand"   1
"zero"  7
"zoho"  1
"zone"  2

问题

是否需要进行一些初始配置才能从 MRJob 获得全局排序的输出?

【问题讨论】:

    标签: python python-3.x mapreduce mrjob


    【解决方案1】:

    您缺少组合器步骤,在本指南中它是单步作业的第一个示例:https://mrjob.readthedocs.io/en/latest/guides/writing-mrjobs.html

    我将复制代码以确保答案的完整性:

    from mrjob.job import MRJob
    import re
    
    WORD_RE = re.compile(r"[\w']+")
    
    
    class MRWordFreqCount(MRJob):
    
        def mapper(self, _, line):
            for word in WORD_RE.findall(line):
                yield word.lower(), 1
    
        def combiner(self, word, counts):
            yield word, sum(counts)
    
        def reducer(self, word, counts):
            yield word, sum(counts)
    
    
    if __name__ == '__main__':
        MRWordFreqCount.run()
    

    【讨论】:

    • 组合器是 mapreduce 作业的可选阶段,用于减少网络拥塞。我相信这不是我问题的根源。无论如何,我尝试添加一个组合器,但结果仍未排序。
    • 哦,对了,对不起,算法似乎对输出进行排序,直到达到一定的大小,然后它就不再被打扰了。您是否尝试过对输出进行后处理?我的意思是这样的stackoverflow.com/a/5737935
    猜你喜欢
    • 1970-01-01
    • 2015-05-13
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多