(所有源码均在 https://github.com/zongzhec/HadoopPractise)
Mapreduce是一个分布式运算程序的编程框架,是用户开发“基于hadoop的数据分析应用”的核心框架。
Mapreduce核心功能是将用户编写的业务逻辑代码和自带默认组件整合成一个完整的分布式运算程序,并发运行在一个hadoop集群上。
解释: 如果让我们自己写一个分布式运算程序,那可就太让人头疼了。比如任务之间的相互通信,不同线程之间的等待和相互唤醒等等等等。但是用了MapReduce框架之后,我们只需要操心业务代码就可以了。
(优点就是两个字:简单!)
(缺点就是一个字:慢!)
MapReduce不擅长做实时计算、流式计算、DAG(有向图)计算。
1)分布式的运算程序往往需要分成至少2个阶段。
2)第一个阶段的maptask并发实例,完全并行运行,互不相干。
3)第二个阶段的reduce task并发实例互不相干,但是他们的数据依赖于上一个阶段的所有maptask并发实例的输出。
4)MapReduce编程模型只能包含一个map阶段和一个reduce阶段,如果用户的业务逻辑非常复杂,那就只能多个mapreduce程序,串行运行。
一个完整的mapreduce程序在分布式运行时有三类实例进程:
1)MrAppMaster:负责整个程序的过程调度及状态协调。
2)MapTask:负责map阶段的整个数据处理流程。
3)ReduceTask:负责reduce阶段的整个数据处理流程。
用户编写的程序分成三个部分:Mapper、Reducer和Driver。
1)Mapper阶段
(1)用户自定义的Mapper要继承自己的父类
(2)Mapper的输入数据是KV对的形式(KV的类型可自定义)
(3)Mapper中的业务逻辑写在map()方法中
(4)Mapper的输出数据是KV对的形式(KV的类型可自定义)
(5)map()方法(maptask进程)对每一个
2)Reducer阶段
(1)用户自定义的Reducer要继承自己的父类
(2)Reducer的输入数据类型对应Mapper的输出数据类型,也是KV
(3)Reducer的业务逻辑写在reduce()方法中
(4)Reducetask进程对每一组相同k的
3)Driver阶段
整个程序需要一个Drvier来进行提交,提交的是一个描述了各种必要信息的job对象
mapper类(注意点在注释里):
package foo.zongzhe.map_reduce.word_count;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
import static foo.zongzhe.utils.Constants.SPACE;
/**
* Mapper阶段编程规范:
* 1. 用户自定义的Mapper要继承自己的父类
* 2. MApper的输入数据是KV对的形式(KV的类型可以自己定义)
* 3. Mapper中的业务逻辑写在map()方法里
* 4. Mapper的输出数据是KV对的形式(KV的类型可以自己定义)
* 5. map()方法(MapTask进程)对每一个调用一次
*/
public class WordCountMapper extends Mapper {
/*
* LongWritable 是input数据在文件中的偏移量
* Text(Input)是读进来的字符串
* Text(Output)是WordCount输出的Key
* IntWritable 是统计的个数
* */
private Text word = new Text();
private IntWritable one = new IntWritable(1);
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
// 把一行数据转换成String
String line = value.toString();
String[] words = line.split(SPACE);
// 遍历数组,把单次编程(word, 1)的形式,再返还给框架
for (String word : words) {
this.word.set(word);
context.write(this.word, one);
/*
* 注意:上一行可以写成:context.write(new Text(word), new IntWritable(1));
* 但是没读进来一行就会new两个对象,最终会导致程序变慢甚至崩溃
* */
// context.write(new Text(word), new IntWritable(1));
}
}
}
Reducer类:
package foo.zongzhe.map_reduce.word_count;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class WordCountReducer extends Reducer {
private IntWritable total = new IntWritable();
@Override
protected void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable value : values) {
sum += value.get();
}
this.total.set(sum);
context.write(key, total);
}
}
Driver类(套路化严重,实际上是最好写的类):
package foo.zongzhe.map_reduce.word_count;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
/**
* Driver 的套路化非常突出,因此也是最好写的一个类
*/
public class WordCountDriver {
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
WordCountDriver wd = new WordCountDriver();
wd.wcDriver(args);
}
public void wcDriver(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
// 获取一个MR job实例
Job job = Job.getInstance(new Configuration());
// 设置类路径
job.setJarByClass(WordCountDriver.class);
// 设置Mapper和Reducer
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
// 设置Mapper和Reducer的输出类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// 设置输入输出数据
FileInputFormat.setInputPaths(job, new Path(args[0])); //FileInputFormat导包不要导错了
FileOutputFormat.setOutputPath(job, new Path(args[1]));
// 提交job
boolean success = job.waitForCompletion(true);
System.exit(success ? 0 : 1);
}
}
程序运行情况:
结果在output文件里的part-r-00000: