【问题标题】:finding the smallest number hadoop streaming python找到最小数量的hadoop流python
【发布时间】:2012-10-04 18:46:08
【问题描述】:

我是 hadoop 框架和 map reduce 抽象的新手。

基本上,我想在一个巨大的文本文件中找到最小的数字(用“,”分隔)

所以,这是我的代码 映射器.py

 #!/usr/bin/env python

 import sys

 # input comes from STDIN (standard input)
 for line in sys.stdin:
 # remove leading and trailing whitespace
 line = line.strip()
 # split the line into words
numbers = line.split(",")
# increase counters
for number in numbers:
    # write the results to STDOUT (standard output);
    # what we output here will be the input for the
    # Reduce step, i.e. the input for reducer.py
    #
    # tab-delimited; the trivial word count is 1
    print '%s\t%s' % (number, 1)

减速器

  #!/usr/bin/env python

from operator import itemgetter
import sys
smallest_number = sys.float_info.max
for line in sys.stdin:
# remove leading and trailing whitespace
     line = line.strip()

# parse the input we got from mapper.py
     number, count = line.split('\t', 1)
     try:
           number = float(number)
     except ValueError:
            continue

     if number < smallest_number:
        smallest_number = number
        print smallest_number <---- i think the error is here... there is no key value thingy

     print smallest_number

我得到的错误:

       12/10/04 12:07:22 ERROR streaming.StreamJob: Job not successful. Error: NA
      12/10/04 12:07:22 INFO streaming.StreamJob: killJob...
          Streaming Command Failed!

【问题讨论】:

  • 你得到什么样的结果?有什么问题?你在说什么“关键值”?
  • @Junuxx:嗨..我刚刚发布了错误..基本上..地图如何减少在文本文件中查找最小数字的抽象看起来像?/我正在谈论的错误是.. mapper 给出的 (number,1) 格式与 word count 示例中的 mapper 基本相同。在 reducer 中,我只关心数字。我取数字并将其与当前最小的数字进行比较并进行交换?
  • 在没有 Hadoop 的情况下调试可能会有所帮助:cat input | ./mapper.py | sort | ./reducer.py 这是否运行成功?
  • @MattD:不,我得到了这个回声“1,2,44,2”| mapper.py :没有这样的文件或目录我做了 chmod +x mapper.py 并且我在同一个目录中?我不知道为什么它找不到文件
  • @MattD:谢谢。我发现了错误:)

标签: python hadoop mapreduce hadoop-streaming


【解决方案1】:

首先,我希望您注意,除非您只使用一个减速器,否则您的解决方案将不起作用。事实上,如果你使用多个 reducer,那么每个 reducer 都会吐出它接收到的最小数字,最终你会得到一个以上的数字。但是接下来的问题是,如果我必须只使用一个 reducer 来解决这个问题(即只有一个任务),那么使用 MapReduce 可以获得什么?这里的诀窍是映射器将并行运行。另一方面,您不希望映射器输出读取的每个数字,否则一个减速器将不得不查看整个数据,这对顺序解决方案没有任何改进。解决这个问题的方法是让每个映射器只输出它读取的最小数字。此外,由于您希望所有 mapper 输出到同一个 reducer,因此 mapper 输出 key 在所有 mapper 中必须相同。

映射器将如下所示:

#!/usr/bin/env python                              

import sys

smallest = None
for line in sys.stdin:
  # remove leading and trailing whitespace          
  line = line.strip()
  # split the line into words                       
  numbers = line.split(",")
  s = min([float(x) for x in numbers])
  if smallest == None or s < smallest:
    smallest = s

print '%d\t%f' % (0, smallest)

减速器:

#!/usr/bin/env python                                           

import sys

smallest = None
for line in sys.stdin:
  # remove leading and trailing whitespace                       
  line = line.strip()
  s = float(line.split('\t')[1])
  if smallest == None or s < smallest:
    smallest = s

print smallest

还有其他可能的方法来解决这个问题,例如使用 MapReduce 框架本身对数字进行排序,以便 reducer 接收到的第一个数字是最小的。如果您想了解更多 MapReduce 编程范式,可以阅读this tutorial with examples, from my blog

【讨论】:

    猜你喜欢
    • 2015-09-28
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 2023-03-18
    • 1970-01-01
    • 2018-06-29
    相关资源
    最近更新 更多