MapReduce-Counter使用

在普通的java程序中我们可以定义一个全局的静态变量,然后我们可以在各个类中去使用,实现累加器的功能,然而在mapruduce中怎么实现这一功能呢,各个map可能运行在不同的JVM中(这里不考虑JVM重用的情况),然而我们可以借助MapReduce提供的Counter功能来实现这一功能,下面我们通过一个实例来说明这一个用法。
实验要求:快速实现文件行数,以及其中错误记录的统计
实验数据:
1
2
error
3
4
5
error
6
7
8
9
10
error
11
12
13
14
error
15
16
17
18
19
解决思路:
定义一个枚举类型,每次调用map函数时,对值进行判断,把判断的结果分别写入不同的Counter,最后输出Counter的值
根据以上步骤下面是实现代码:

map阶段:

  1. import java.io.IOException;  
  2. import org.apache.hadoop.io.IntWritable;  
  3. import org.apache.hadoop.io.LongWritable;  
  4. import org.apache.hadoop.io.Text;  
  5. import org.apache.hadoop.mapreduce.Mapper;  
  6.   
  7. public class MyMapper extends Mapper<LongWritable, Text, LongWritable, IntWritable> {  
  8.     /** 
  9.      * 定义一个枚举类型 
  10.      * @date 2016年3月25日 下午3:29:44  
  11.      * @{tags} 
  12.      */  
  13.     public static enum FileRecorder{  
  14.         ErrorRecorder,  
  15.         TotalRecorder  
  16.     }  
  17.     @Override  
  18.     protected void map(LongWritable key, Text value, Context context)  
  19.             throws IOException, InterruptedException {  
  20.         if("error".equals(value.toString())){  
  21.             /** 
  22.              * 把counter实现累加 
  23.              */  
  24.             context.getCounter(FileRecorder.ErrorRecorder).increment(1);  
  25.         }  
  26.         /** 
  27.          * 把counter实现累加 
  28.          */  
  29.         context.getCounter(FileRecorder.TotalRecorder).increment(1);  
  30.     }  
  31. }  
启动函数:
  1. import org.apache.hadoop.conf.Configuration;  
  2. import org.apache.hadoop.fs.FileSystem;  
  3. import org.apache.hadoop.fs.Path;  
  4. import org.apache.hadoop.io.IntWritable;  
  5. import org.apache.hadoop.io.LongWritable;  
  6. import org.apache.hadoop.mapreduce.Job;  
  7. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  8. import org.apache.hadoop.mapreduce.lib.input.NLineInputFormat;  
  9. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  10. import com.seven.mapreduce.counter.MyMapper.FileRecorder;  
  11. public class JobMain {  
  12.     public static void main(String[] args) throws Exception {  
  13.         Configuration configuration = new Configuration();  
  14.         /** 
  15.          * 使NLineInputFormat来分割一个小文件,近而模拟分布式大文件的处理 
  16.          */  
  17.         configuration.setInt("mapreduce.input.lineinputformat.linespermap"5);   
  18.         Job job = new Job(configuration, "counter-job");  
  19.         job.setInputFormatClass(NLineInputFormat.class);    
  20.         job.setJarByClass(JobMain.class);  
  21.         job.setMapperClass(MyMapper.class);  
  22.         job.setMapOutputKeyClass(LongWritable.class);  
  23.         job.setMapOutputValueClass(IntWritable.class);  
  24.         FileInputFormat.addInputPath(job, new Path(args[0]));  
  25.         Path outputDir = new Path(args[1]);  
  26.         FileSystem fs = FileSystem.get(configuration);  
  27.         if( fs.exists(outputDir)) {  
  28.             fs.delete(outputDir ,true);  
  29.         }  
  30.         FileOutputFormat.setOutputPath(job, outputDir);  
  31.         if(job.waitForCompletion(true) ? truefalse) {  
  32.             System.out.println("Error num:" + job.getCounters().findCounter(FileRecorder.ErrorRecorder).getValue());  
  33.             System.out.println("Total num:" + job.getCounters().findCounter(FileRecorder.TotalRecorder).getValue());  
  34.         }  
  35.     }  
  36. }  
运行结果:



总结:

由上可以看出总共跑了5个map任务,而且通过Counter实现了不同JVM中的全局累加器的功能。关于除自定义Counter以外的其它Counter的含义

原文地址:http://blog.csdn.net/doegoo/article/details/50981196

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值