Mapreduce是一个计算框架,既然是作计算的框架,那么表现形式就是有个输入(input)。mapreduce操做这个输入(input),经过自己定义好的计算模型,获得一个输出(output),这个输出就是咱们所须要的结果。java
在运行一个mapreduce计算任务时候,任务过程被分为两个阶段:map阶段和reduce阶段,每一个阶段都是用键值对(key/value)做为输入(input)和输出(output)。而咱们程序员要作的就是定义好这两个阶段的函数:map函数和reduce函数。程序员
讲解mapreduce运行原理前,首先咱们看看mapreduce里的hello world实例WordCount,这个实例在任何一个版本的hadoop安装程序里都会有。apache
package org.apache.hadoop.examples; 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.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; import org.apache.hadoop.util.GenericOptionsParser; /** * * 描述:WordCount explains by York * @author Hadoop Dev Group */ publicclass WordCount { /** * 创建Mapper类TokenizerMapper继承自泛型类Mapper * Mapper类:实现了Map功能基类 * Mapper接口: * WritableComparable接口:实现WritableComparable的类能够相互比较。全部被用做key的类应该实现此接口。 * Reporter 则可用于报告整个应用的运行进度,本例中未使用。 * */ public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{ /** * IntWritable, Text 均是 Hadoop 中实现的用于封装 Java 数据类型的类,这些类实现了WritableComparable接口, * 都可以被串行化从而便于在分布式环境中进行数据交换,你能够将它们分别视为int,String 的替代品。 * 声明one常量和word用于存放单词的变量 */ privatefinalstatic IntWritable one =new IntWritable(1); private Text word =new Text(); /** * Mapper中的map方法: * void map(K1 key, V1 value, Context context) * 映射一个单个的输入k/v对到一个中间的k/v对 * 输出对不须要和输入对是相同的类型,输入对能够映射到0个或多个输出对。 * Context:收集Mapper输出的<k,v>对。 * Context的write(k, v)方法:增长一个(k,v)对到context * 程序员主要编写Map和Reduce函数.这个Map函数使用StringTokenizer函数对字符串进行分隔,经过write方法把单词存入word中 * write方法存入(单词,1)这样的二元组到context中 */ public void map(Object key, Text value, Context context) throws IOException, InterruptedException { StringTokenizer itr =new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); } } } public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> { private IntWritable result =new IntWritable(); /** * Reducer类中的reduce方法: * void reduce(Text key, Iterable<IntWritable> values, Context context) * 中k/v来自于map函数中的context,可能通过了进一步处理(combiner),一样经过context输出 */ public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum =0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } publicstaticvoid main(String[] args) throws Exception { /** * Configuration:map/reduce的j配置类,向hadoop框架描述map-reduce执行的工做 */ Configuration conf =new Configuration(); String[] otherArgs =new GenericOptionsParser(conf, args).getRemainingArgs(); if (otherArgs.length !=2) { System.err.println("Usage: wordcount <in> <out>"); System.exit(2); } Job job =new Job(conf, "word count"); //设置一个用户定义的job名称 job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); //为job设置Mapper类 job.setCombinerClass(IntSumReducer.class); //为job设置Combiner类 job.setReducerClass(IntSumReducer.class); //为job设置Reducer类 job.setOutputKeyClass(Text.class); //为job的输出数据设置Key类 job.setOutputValueClass(IntWritable.class); //为job输出设置value类 FileInputFormat.addInputPath(job, new Path(otherArgs[0])); //为job设置输入路径 FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//为job设置输出路径 System.exit(job.waitForCompletion(true) ?0 : 1); //运行job } }
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {…}
这里有三个参数,前面两个Object key, Text value 就是输入的 key 和 value ,第三个参数Context context这是能够记录输入的key和value,例如:context.write(word, one)。app
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {…}
reduce函数的输入也是一个 key/value 的形式,不过它的value是一个迭代器的形式 Iterable<IntWritable> values ,也就是说reduce的输入是一个key对应一组的值的value,reduce也有context和map的context做用一致。至于计算的逻辑则须要程序员编码实现。框架
首先是分布式
Configuration conf = new Configuration();
运行MapReduce程序前都要初始化Configuration,该类主要是读取MapReduce系统配置信息,这些信息包括hdfs还有MapReduce,也就是安装hadoop时候的配置文件例如:core-site.xml、hdfs-site.xml和mapred-site.xml等等文件里的信息,有些童鞋不理解为啥要这么作,这个是没有深刻思考MapReduce计算框架形成,咱们程序员开发MapReduce时候只是在填空,在map函数和reduce函数里编写实际进行的业务逻辑,其它的工做都是交给MapReduce框架本身操做的,可是至少咱们要告诉它怎么操做啊,好比hdfs在哪里,MapReduce的jobstracker在哪里,而这些信息就在conf包下的配置文件里。函数
接下来的代码是:oop
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); if (otherArgs.length != 2) { System.err.println("Usage: wordcount <in> <out>"); System.exit(2); }
If的语句好理解,就是运行WordCount程序时候必定是两个参数,若是不是就会报错退出。至于第一句里的GenericOptionsParser类,它是用来解释经常使用hadoop命令,并根据须要为Configuration对象设置相应的值,其实平时开发里咱们不太经常使用它,而是让类实现Tool接口,而后再main函数里使用ToolRunner运行程序,而ToolRunner内部会调用GenericOptionsParser。编码
接下来的代码是:spa
Job job = new Job(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class);
第一行就是在构建一个job,在mapreduce框架里一个mapreduce任务也叫mapreduce做业也叫作一个mapreduce的job,而具体的map和reduce运算就是task了,这里咱们构建一个job,构建时候有两个参数,一个是conf这个就不累述了,一个是这个job的名称。
第二行就是装载程序员编写好的计算程序,例如咱们的程序类名就是WordCount了。这里我要作下纠正,虽然咱们编写mapreduce程序只须要实现map函数和reduce函数,可是实际开发咱们要实现三个类,第三个类是为了配置mapreduce如何运行map和reduce函数,准确的说就是构建一个mapreduce能执行的job了,例如WordCount类。
第三行和第五行就是装载map函数和reduce函数实现类了,这里多了个第四行,这个是装载Combiner类,这个类和mapreduce运行机制有关,其实本例去掉第四行也没有关系,可是使用了第四行理论上运行效率会更好。
接下来的代码:
job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class);
这个是定义输出的 key/value 的类型,也就是最终存储在 hdfs 上结果文件的 key/value 的类型。
最后的代码是:
FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1);
第一行就是构建输入的数据文件,第二行是构建输出的数据文件,最后一行若是job运行成功了,咱们的程序就会正常退出。