mapreduce怎么写( 二 )

4. 多语言编写MapReduce作业 Hadoop采用java编写 , 因而Hadoop天生支持java语言编写作业 , 但在实际应用中 , 有时候 , 因要用到非java的第三方库或者其他原因 , 要采用C/C++或者其他语言编写MapReduce作业 , 这时候可能要用到Hadoop提供的一些工具 。如果你要用C/C++编写MpaReduce作业 , 可使用的工具有Hadoop Streaming或者Hadoop Pipes 。
如果你要用Python编写MapReduce作业 , 可以使用Hadoop Streaming或者Pydoop 。如果你要使用其他语言 , 如shell,php,ruby等 , 可使用Hadoop Streaming 。
关于Hadoop Streaming编程 , 可参见我的这篇博文:《Hadoop Streaming编程》(/projects/pydoop/ 关于Hadoop pipes编程 , 可参见《Hadoop Tutorial 2.2 — Running C++ Programs on Hadoop》 。5. 编程方式比较 (1)java 。
Hadoop支持的最好最全面的语言 , 而且提供了很多工具方便程序员开发 。(2)Hadoop Streaming 。
它最大的优点是支持多种语言 , 但效率较低 , reduce task需等到map 阶段完成后才能启动;它不支持用户自定义InputFormat , 如果用户想指定输入文件格式 , 可使用java语言编写或者在命令行中指定分隔符;它采用标准输入输出让C/C++与java通信 , 因而只支持text数据格式 。(3)Hadoop Pipes 。
专门为C/C++语言设计 , 由于其采用了socket方式让C/C++与java通信 , 因而其效率较低(其优势在于 , 但作业需要大量 , 速度很快) 。它支持用户(用C/C++)编写RecordReader 。
(4)Pydoop 。它是专门方便python 。
2.怎么用Python写mapreduce,请举例说明,初学者,请赐教,不胜感激1.lambda# 匿名函数# 基本用法 lambda x: x**2 # 第一个参数 , 然后是表达式# 也可以使用如下(lambda x: x**2)(5)2. map()def map(function, sequence, *sequence_1): # real signature unknown; restored from __doc__ """ map(function, sequence[, sequence,。
]) -> list Return a list of the results of applying the function to the items of the argument sequence(s). If more than one sequence is given, the function is called with an argument list consisting of the corresponding item of each sequence, substituting None for missing values when not all sequences have the same length. If the function is None, return a list of the items of the sequence (or a list of tuples if more than one sequence). """ return []# 两个参数 , 一个处理函数 , 一个可迭代的序列# 返回一个列表# 例如 计算1到10的平方 , 并以列表的形式返回map(lambda x: x**2, range(1, 11))# 结果如下[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]# 当然 也可以如下这样使用def square(x): return x**2map(square, range(1, 11))3.reduce()def reduce(function, sequence, initial=None): # real signature unknown; restored from __doc__ """ reduce(function, sequence[, initial]) -> value Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. """ pass# 两个参数 , 一个接受两个参数的函数 , 一个序列参数# 例如 计算 1到10 的和reduce(lambda x, y: x+y, range(1, 11))# 当然 , 不适用lambda匿名函数也可以def add(x, y): return x+yreduce(add, range(1, 11))# 结果如下454.filter()def filter(function_or_none, sequence): # known special case of filter """ filter(function or None, sequence) -> list, tuple, or string Return those items of sequence for which function(item) is true. If function is None, return the items that are true. If sequence is a tuple or string, return the same type, else return a list. """ pass# 接受两个参数 , 一个过滤函数 , 返回True 或者 False ,  以及一个序列# 例如 ,  计算100以内的偶数filter(lambda x: x % 2 == 0, range(100))# 如上def div2(x): if x % 2 == 0: return True else: return Falsefilter(div2, range(100))# 结果如下 [0, 2, 4, 6, 8, 10, 12, 14, 16,。] 。