Hadoop单词统计-各个过程详细说明

package hadoop01;

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.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;

public class WordCount {

    /**
     * 一:调用Map执行
     * map阶段
     * 输入:行数为Key,正行的内容为value
     * 在map函数中,会对输入的值进行分割处理
     * 输出:以<key,value>的形式输出数据。例如<"hello",1>;
     */
    public static class WordCountmapper extends Mapper<Object, Text, Text, IntWritable>{
        private final static IntWritable one = new IntWritable();
        private Text word = new Text();
        public void map(Object key,Text value,Context context) throws IOException, InterruptedException{
            String [] words = value.toString().split(" ");
            for(String str : words){
                word.set(str);
                context.write(word, one);
            }
        }
    }

    /**
     * 二:Map函数执行过程中
     * map端输出的数据首先会存储在内存缓冲区中,当超出溢写阀值是,会将内存中的文件溢写到本地文件系统
     * 1.在内存中,首先会进行partation操作,目的是将不同的key值分配到不同的reduce任务上,来进行负载均衡,默认的partation方法是Hash模运算
     * 2.在溢写发生时,首相会对数据进行sort归并排序操作,产生的结果应该为<"hello",{1,1,1,1,1}>的形式
     * 3.如果设置了Combiner,现在就会执行Combiner函数,进行map端的Combiner操作
     * 4.将执行结果溢写到本地文件系统 
     */
    /**
     * 三:Map函数执行完毕
     *  Map函数执行完毕后,可能会产生多个溢写文件,此时会对多个溢写文件进行合并操作
     *  在合并文件的过程中,也可能进行Combiner操作
     */
    /**
     * 四:执行Reduce操作
     * Reduce操作会把Map端的输出结果文件进行最终的合并,生成最终的结果
     */
    public static class WorldCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{

        public void reduce(Text key,Iterable<IntWritable> value,Context context) throws IOException, InterruptedException{
            int total = 0;
            for(IntWritable val : value){
                total++;
            }
            context.write(key, new IntWritable(total));
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration configuration = new Configuration();
        Job job = new Job(configuration,"word count");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(WordCountmapper.class);
        job.setReducerClass(WorldCountReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileInputFormat.addInputPath(job, new Path("/user/root/books"));
        FileOutputFormat.setOutputPath(job, new Path("/user/root/bookout"));
        System.exit(job.waitForCompletion(true)?0:1);
    }

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值