zoukankan      html  css  js  c++  java
  • 大数据入门——对WordCount的改进

    package com.imooc.hadoop.mapreduce;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    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;
    
    import java.io.IOException;
    
    /**
     * 使用MapReduce开发WordCount应用程序
     */
    public class CombinerApp {
    
        /**
         * Map:读取输入的文件
         */
        public static class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable>{
    
            LongWritable one = new LongWritable(1);
    
            @Override
            protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
    
                // 接收到的每一行数据
                String line = value.toString();
    
                //按照指定分隔符进行拆分
                String[] words = line.split(" ");
    
                for(String word :  words) {
                    // 通过上下文把map的处理结果输出
                    context.write(new Text(word), one);
                }
    
            }
        }
    
        /**
         * Reduce:归并操作
         */
        public static class MyReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
    
            @Override
            protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
    
                long sum = 0;
                for(LongWritable value : values) {
                    // 求key出现的次数总和
                    sum += value.get();
                }
    
                // 最终统计结果的输出
                context.write(key, new LongWritable(sum));
            }
        }
    
        /**
         * 定义Driver:封装了MapReduce作业的所有信息
         */
        public static void main(String[] args) throws Exception{
    
            //创建Configuration
            Configuration configuration = new Configuration();
    
            // 准备清理已存在的输出目录
            Path outputPath = new Path(args[1]);
            FileSystem fileSystem = FileSystem.get(configuration);
            if(fileSystem.exists(outputPath)){
                fileSystem.delete(outputPath, true);
                System.out.println("output file exists, but is has deleted");
            }
    
            //创建Job
            Job job = Job.getInstance(configuration, "wordcount");
    
            //设置job的处理类
            job.setJarByClass(CombinerApp.class);
    
            //设置作业处理的输入路径
            FileInputFormat.setInputPaths(job, new Path(args[0]));
    
            //设置map相关参数
            job.setMapperClass(MyMapper.class);
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(LongWritable.class);
    
            //设置reduce相关参数
            job.setReducerClass(MyReducer.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(LongWritable.class);
    
            //通过job设置combiner处理类,其实逻辑上和我们的reduce是一模一样的
            job.setCombinerClass(MyReducer.class);
    
            //设置作业处理的输出路径
            FileOutputFormat.setOutputPath(job, new Path(args[1]));
    
            System.exit(job.waitForCompletion(true) ? 0 : 1);
        }
    }

    增加了Combiner,为了使程序运行的速度更快。

    Combiner运行机制

  • 相关阅读:
    神策Loagent数据收集 windows部署的坑
    hive算法报错..
    检查SQL语句是否合法
    This operation is not available unless admin mode is enabled: FLUSHDB
    关于scrollLeft的获取在不同浏览器或相同浏览器的不同版本下的获取
    Vue-cli创建项目从单页面到多页面4
    Vue-cli创建项目从单页面到多页面3-关于将打包后的项目文件不放在根目录下
    Vue-cli创建项目从单页面到多页面2-history模式
    Vue-cli创建项目从单页面到多页面
    关于网络硬件配置出现问题,无法上网问题的解决
  • 原文地址:https://www.cnblogs.com/aishanyishi/p/9479722.html
Copyright © 2011-2022 走看看