Hadoop 0.20.0中包含了新的API:org.apache.hadoop.mapreduce包:
充分使用上下文对象,是用户代码能与MapReduce系统通信,如MapContext基本具备JobConf、OutputCollector和Reporter的功能;同时支持push和pull式的迭代,这两类API均可以将key/value对记录push给mapper,同时还允许吧记录从map中pull出【示例??】,可以实现数据的批量而非逐条处理。
combiner:
集群中的可用带宽将限制MapReduce作业的数量,所以应尽量避免map任务和reduce任务之间的数据传输。可以用combiner函数在map端将其输出合并,在传给reduce作为其输入。但combiner的使用也有限制:1. combiner输出的key/value类型必须与reduce的输入类型一致;2. reduce的输出结果必须保持一致,比如计算一系列数据的最大值和平均值,使用combiner后,前者的reduce结果将不变,而后者将有所差别。
Streaming:
允许使用非Java语言写map和reduce函数,streaming使用unix标准标准流作为Hadoop和应用程序之间的接口,我们可以通过标准输入/输出写MapReduce程序。
# map
for line in sys.stdin:
# ...
# print ...
# reduce
for line in sys.stdin:
# ...
# print...
以上为python的简单示例,通过下式执行
%hadoop jar hadoop-*-streaming.jar -input in -output out -mapper "map.py | sort | reduce.py" -reducer reduce.py
Pipes:
C++接口,使用套接字作为tasktracker与C++版本map和reduce函数的进程之间的通信,而未使用JNI。
#include "hadoop/Pipes.hh"
#include "hadoop/TemplateFactory.hh"
#include "hadoop/StringUtils.hh"
class Mapper : public HadoopPipes::Mapper {
public:
Mapper(HadoopPipes::TaskContext& context) {
}
void map(HadoopPipes::MapContext& context) {
// std::string line = context.getInputValue();
//...
// context.emit(key, value)
}
};
class Reducer : public HadoopPipes::Reducer {
public:
Reducer(HadoopPipes::TaskContext& context) {
}
void map(HadoopPipes::ReduceContext& context) {
while(context.nextValue()) {
// HadoopUtil::toInt(context.getInputValue());
// context.emit(key, value)
}
}
};
int main(int argc, char *argv[]) {
return HadoopPipes::runTask(HadoopPipes::TemplateFactory<Mapper, Reucer>());
}
不能在独立模式(本地运行)下运行,它依赖Hadoop的分布式缓存机制。
%hadoop pipes -D hadoop.pipes.java.recordrader=true -D hadoop.pipes.java.recordwriter=true -input in -output out -program bin