本文共 3581 字,大约阅读时间需要 11 分钟。
Wordcount 号称Hadoop中的HelloWord,花时间好好研究了以下程序的细节,这研究之前必须搞懂MapReduce工作原理,网上有很多讲解。其实MapReduce就是个分治的思想,将文件分在不同的从节点上进行处理(Map),然后排序合并,最后进行汇总的过程(Reduce)。
MapReduce的总体流程如下:
研究代码的过程中Reduce程序段不太懂,查看了Hadoop官网,官网的解释还是最好的,以后对于不会的知识要学会在官网找答案。
下面送上官网链接:
Map和Reduce两主类对应的参数解释:
下面是代码(java)的详细解释:
package com.yootk.mr.demo;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IntWritable;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;import org.apache.hadoop.io.Text;/** * * it is a note. * @author *需要在新建工程中提前配置(导入)hadoop文件中的jar包,/hadoop2.7.3-share-hadoop/common hadoop2.7.3-share-hadoop/common/lib hadoop2.7.3-share-hadoop/common/jdiff hadoop2.7.3-share-hadoop/mapreduce hadoop2.7.3-share-hadoop/mapreduce/lib * Data: 2017年5月23日下午3:21:05 */public class Wordcount { //实现单词统计 //最为关键的Map部分与Reduce部分,需要用户自己来写 /** * 本操作主要是进行map的数据处理 * 在Mapper的父类里面接收的内容如下: * Object:输入数据的具体内容; * Text:每行的文本数据; * Text:每个单词分解后的统计结果; * IntWritable:输出Map记录的结果; * * it is a note. * @author * * Data: 2017年5月23日下午3:22:57 */ private static class WordcountMapper extends Mapper