MapReduce典型编程场景——全局计数器

1、介绍
计数器是用来记录job的执行进度和状态的。它的作用可以理解为日志,我们可以在程序的某个位置插入计数器,记录数据或者进度的变化情况。
MapReduce计数器(counter)为我们提供一个窗口,用于观察MapReduce Job运行期的各种细节数据。对MapReduce性能调优有很大的帮助,MapReduce性能优化的评估大部分都是基于这些counter的数值表现出来的。
MapReduce自带了许多默认Counter,现在我们来分析这些默认Counter的含义,方便大家观察Job结果。


2、需求

在实际生产代码中,常常需要将数据处理过程中遇到的不合规数据进行全局技术,类似于这种需求可以借助MapReduce框架中提供的全局计数器来实现。


3、实现
利用全局计数器统计一个目录下所有文件出现的单词总数和总行数。
import java.io.IOException;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class CounterWordCount {

     enum CouterWordCountC{
               COUNT_WORDS, COUNT_LINES
      }

     public static void main(String[] args) throws Exception {
               // 指定 hdfs 相关的参数
              Configuration conf = new Configuration();
              Job job = Job.getInstance(conf);

              // 设置 jar 包所在路径
              job.setJarByClass(CounterWordCount.class);
              job.setMapperClass(WCCounterMapper.class);
              job.setMapOutputKeyClass(Text.class);
              job.setMapOutputValueClass(LongWritable.class);

              // 本地路径
              Path inputPath = new Path("d:/wordcount/input");
              FileInputFormat.setInputPaths(job, inputPath);
              job.setNumReduceTasks(0);
             Path outputPath = new Path("d:/wordcount/output");
             FileSystem fs = FileSystem.get(conf);
             if(fs.exists(outputPath)){
                        fs.delete(outputPath, true);
             }
             FileOutputFormat.setOutputPath(job, outputPath);
              
            // 最后提交任务
            boolean waitForCompletion = job.waitForCompletion(true);
            System.exit(waitForCompletion?0:1);
       }

       private static class WCCounterMapper extends Mapper{
            @Override
            protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

                  // 统计行数,因为默认读取文本是逐行读取,所以 map 执行一次,行数+1
                  context.getCounter(CouterWordCountC.COUNT_LINES).increment(1L);
                  (increment===增长,增量)
                  String[] words = value.toString().split(" ");
                  for(String word: words){
                          // 统计单词总数,遇见一个单词就+1
                          context.getCounter(CouterWordCountC.COUNT_WORDS).increment(1L);
                   }
           }
       }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值