一、MapReduce程序一般通过键值对的形式来处理数据

    

Map:(K1,V1)----->List(K2,V2)
 Reduce:(K2,List(V2))------>List(K3,V3)

 

二、数据流动

split的方式,被分发到各个节点上

Map任务在一个split上面进行处理。

Mapper任务输出中间数据

shuffle),节点之间进行数据交换

key值的中间数据(键值对)被送到同样的reducer任务中

reduce执行任务后,输出结果。

Map过程,后两步为Reduce过程

三、MapReduce程序使用的数据类型

MapReduce框架需要将数据在集群中进行移动,所以框架定义了一种序列化的键值对类型,即keys和values必须能够进行序列化。所以实现了Writable接口的对象可以充当values,实现了WritableComparalbe<T>接口的对象可以充当keys或values

WritableComparable<T>接口的类有如下几个:

     

BooleanWritable、ByteWritable、DoubleWritable、FloatWritable、IntWritable、LongWritable、Text、NullWritable。



 

 

四:Mapper

mapper,.必须实现Mapper接口并且继承MapReduceBase类,  MapReduceBase类是mappers和reduces类的基类,它包含了构造方法和析构方法。

void configure(JobConf job):提取配置文件或程序中设置的参数值

void close() :任务是关闭数据库连接,关闭文件等等

Mapper接口中map函数的原型

Void map(K1 key,V1 value,OutputCollector<K2,V2> output,Reporter reporter) throws IOException



 

 

 

 

Hadoop中实现的Mapper接口的常用类

IdentityMapper<K,V>: 实现了Mapper<K,V,K,V> ,直接将map的输入转换为输出。

InverseMapper<K,V>  实现了Mapper<K,V,V,K>,反转键值对。

RegexMapper<K>    实现了Mapper<K,TEXT,TEXT,LongWritable>,为每一个匹配的正则表达式生成一个(match,1)键值对

TokenCountMapper<K> 实现了Mapper<K,TEXT,TEXT,LongWritable>,当输入值被标记,那么生成一个(token,1)键值对

五、Reducer

mapper,.必须实现Reducer接口并且继承MapReduceBase类 

reduce方法的原型为:

     

void reduce(K2,key,Iterator<V2> values,
                  OutputCollector<K3,V3> output,Reporter reporter) throws IOException



  

 

mapper任务传来的数据,首先将数据进行排序,然后根据key值进行分组,最后调用reduce方法

Hadoop中实现的Reducer接口的常用类

IdentityReducer<K,V>:直接将输入转换为输出

LongSumReducer<K> 实现了Reducer<K,LongWritable,K,LongWritable> ,对于同样的key值进行value值的相加。