1. MapReduce编程模型
一种分布式计算模型框架,解决海量数据的计算问题。
MapReduce将整个并行计算过程抽象到两个函数:
1) Map(映射):对一些独立元素组成的列表的每一个元素进行指定的操作,可以高度并行;
2) Reduce(化简):对一个列表的元素进行合并;
一个简单的MapReduce程序只要制定map()、reduce()、input和output,剩下的事由框架完成。
适合用 MapReduce 来处理的数据集(或任务)有一个基本要求:待处理的数据集可以分解成许多小的数据集,而且每一个小数据集都可以完全并行地进行处理。
Job:用户的每一个计算请求;
Task:拆分出来的执行的单位;
2. MapReduce编程
Map阶段:
1) 输入数据格式的解析,InputFormat;
2) 输入数据处理,Mapper;
3) 数据分组(map函数处理以后的结果数据),Partitioner
Reduce阶段:
1) 数据远程拷贝
2) 数据按照key进行排序
3) 数据处理,reduce函数
4) 数据输出格式(reduce函数处理以后得结果数据),OutputFormat
package org.dragon.hadoop.mr;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
/**
* MapReduce书写例子 wordcount
*/
public class WordCount {
//Mapper 区域
/*
* KEYIN : 输入Key类型
* VALUEIN :输入value类型
* KEYOUT : 输出key类型
* VALUEOUT : 输出value类型
*/
/*
* wordcount 程序 map类
*/
static class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
@Override
protected void map(LongWritable key, Text value,
Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
//获取每行数据的值
String lineValue = value.toString();
//进行分割
StringTokenizer stringTokenizer = new StringTokenizer(lineValue);
while(stringTokenizer.hasMoreTokens()){
String wordValue = stringTokenizer.nextToken();
//设置map 输出的key值
word.set(wordValue);
//上下文输出map的key值和value值
context.write(word, one);
} //end while
}
}
//Reducer 区域
/*
* wordcount 程序 reduce 类
*/
static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
private IntWritable result = new IntWritable();
@Override
protected void reduce(Text key, Iterable<IntWritable> values,
Reducer<Text, IntWritable, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
int sum = 0 ;
for(IntWritable value:values){
sum += value.get();
}
result.set(sum);
context.write(key, result);
}
}
//客户端
public static void main(String[] args) throws Exception {
Configuration configuration = new Configuration();
Job job = new Job(configuration,"wc");
//1、设置job运行的类
job.setJarByClass(WordCount.class);
//2、设置Mapper 和 Reduceer 类
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);
//3、设置输入文件的目录和输出文件的目录
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
//4、设置输出结果key 和 value 的类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
//5、提交job,等待运行结果,并在客户端显示客户端运行信息
boolean isSuccess = job.waitForCompletion(true);
//6、结束程序
System.exit(isSuccess ? 0:1);
}
}