zoukankan      html  css  js  c++  java
  • Hadoop之MapReduce单词计数经典实例

    MapReduce实现单词计数示意图

    这里写图片描述

    MapReduce实现单词计数实例代码(Java)

    ①Mapper

    import java.io.IOException;
    
    import org.apache.commons.lang.StringUtils;
    import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Mapper;
    
    public class WM extends Mapper<LongWritable, Text, Text, LongWritable> {
        protected void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
    
            String line = value.toString();
            String[] words = StringUtils.split(line, " ");
            for (String word : words) {
                context.write(new Text(word), new LongWritable(1));
            }
    
        }
    }

    ②Reducer

    import java.io.IOException;
    
    import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Reducer;
    
    public class WR extends Reducer<Text, LongWritable, Text, LongWritable> {
        protected void reduce(Text word, Iterable<LongWritable> counts,
                Context context) throws IOException, InterruptedException {     
            int counter = 0;
    
            for (LongWritable count : counts) {
                long i = count.get();
                counter += i;
            }
    
            context.write(word, new LongWritable(counter));
    
        }
    }

    ③主程序

    import java.io.IOException;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    import org.apache.hadoop.mapreduce.Job;
    
    public class Launcher {
        public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
            Configuration conf = new Configuration();
    
            Job job = Job.getInstance(conf);
    
            job.setMapperClass(WM.class);
    
            job.setMapOutputKeyClass(Text.class);
    
            job.setMapOutputValueClass(LongWritable.class);
    
            job.setReducerClass(WR.class);
    
            job.setOutputKeyClass(Text.class);
    
            job.setOutputValueClass(LongWritable.class);
    
            job.setJarByClass(Launcher.class);
    
            FileInputFormat.setInputPaths(job, "e://hello//words.txt");
    
            FileOutputFormat.setOutputPath(job, new Path("e://hello//output"));
    
            job.waitForCompletion(true);
        }
    }
  • 相关阅读:
    精心总结的Linux运维面试题汇总
    [DevExpress]DxValidationProvider分享
    PivotGridField 中对Unbound的列赋值(除法)--数据钻取
    PivotGridField 中对Unbound的列赋值(除法)
    2020后端/前段开发工程师应当掌握的专业技能一览(源自github)
    跨域的CRUD
    水印图片和文字
    导入 导出 压缩 解压
    winForm的CRUD 加上传图片 的DAL
    winForm的CRUD 加上传图片
  • 原文地址:https://www.cnblogs.com/wanxi/p/6476217.html
Copyright © 2011-2022 走看看