查找hadoop流式python的最小数目
我不熟悉hadoop框架和map reduce抽象 基本上,我想在一个巨大的文本文件中找到最小的数字(以“,”分隔) 这是我的代码 mapper.py查找hadoop流式python的最小数目,python,hadoop,mapreduce,hadoop-streaming,Python,Hadoop,Mapreduce,Hadoop Streaming,我不熟悉hadoop框架和map reduce抽象 基本上,我想在一个巨大的文本文件中找到最小的数字(以“,”分隔) 这是我的代码 mapper.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() # spli
#!/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
首先,我希望您注意到,除非您只使用一个减速器,否则您的解决方案将不起作用。事实上,如果您使用多个减速机,那么每个减速机将吐出它接收到的最小数字,并且您将得到多个数字。但是接下来的问题是,如果我必须只使用一个reducer来解决这个问题(即,只使用一个任务),那么使用MapReduce可以获得什么?这里的技巧是映射程序将并行运行。另一方面,您不希望映射器输出读取的每个数字,否则一个缩减器将必须查看整个数据,这与顺序解决方案相比没有任何改进。解决这个问题的方法是让每个映射器只输出它读取的最小数字。此外,由于希望所有映射器输出都转到同一个减速机,因此映射器输出键在所有映射器上必须相同 映射器将如下所示:
#!/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
导入系统
最小=无
对于sys.stdin中的行:
#删除前导和尾随空格
line=line.strip()
#把这行分成几个字
数字=行。拆分(“,”)
s=最小值([数字中x的浮动(x)])
如果最小==无或s<最小:
最小=s
打印“%d\t%f%”(0,最小)
减速器:
#!/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
#/usr/bin/env python
导入系统
最小=无
对于sys.stdin中的行:
#删除前导和尾随空格
line=line.strip()
s=float(line.split('\t')[1])
如果最小==无或s<最小:
最小=s
打印最小的
还有其他可能解决此问题的方法,例如使用MapReduce框架本身对数字进行排序,以便reducer接收到的第一个数字最小。如果您想了解更多MapReduce编程范例,可以阅读。您得到了什么样的结果?有什么问题吗?你在说什么“关键价值”呢?@Junuxx:Hi。。我刚刚公布了错误。。基本上。。映射如何减少在文本文件中查找最小数字的抽象性。。mapper给出的(数字,1)格式与word count示例中的mapper基本相同。总之,我关心的只是数字。。我获取数字并将其与当前的最小数字进行比较,然后进行交换?在没有Hadoop的情况下进行调试可能会有所帮助:
cat input |/mapper.py | sort |/reducer.py
这是否成功运行?@MattD:不,我得到的是回音“1,2,44,2”| mapper.py:没有这样的文件或目录我做了chmod+x mapper.py并且我在同一个目录中?我不知道为什么它找不到file@MattD:谢谢。我发现了错误:)
#!/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