默认计数器
- import org.apache.hadoop.mapreduce.TaskCounter; //MapReduce计数器
- import org.apache.hadoop.mapreduce.FileSystemCounter; //文件系统计数器
- import org.apache.hadoop.mapreduce.lib.input.FileInputFormatCounter; // .FileInputFormat计数器
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormatCounter;// FileOutputFormat计数器
import org.apache.hadoop.mapreduce.JobCounter;// job任务计数器
自定义计数器
context.getCOunter(“计数器类型”, “计数器名字”) // 声明计数器 计数器 .increment(1l)
- 通过枚举实现
案列都是wordcount只是在逐渐叠加内容 这里就不放全部代码了
代码演示
```java //我是声明在map里面得 所以结果表示得是 有多少行数据进入了map
//声明对象 Counter counter = context.getCounter(“MySym”, “MapRow”); //每一次加一 counter.increment(1L); //输出结果 /**
MySym MapRow=3 */
```java// //我是声明在reduce里面得 结果没有理解为什么是 8public static enum Counter{//声明枚举My_Reduce, ReduceRow}// 在类中使用context.getCounter(Counter.My_Reduce).increment(1L);// 输出结果/*** MapReduce_one.JobMain$IntSumReducer$Counter* My_Reduce=8*/
map类全部代码
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {private static final IntWritable one = new IntWritable(1);private Text word = new Text();public TokenizerMapper() {}public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {StringTokenizer itr = new StringTokenizer(value.toString(), ",");Counter counter = context.getCounter("MySym", "MapRow");counter.increment(1L);while(itr.hasMoreTokens()) {this.word.set(itr.nextToken());context.write(this.word, one);}}}
reduce类全部代码
public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {public static enum Counter{My_Reduce, ReduceRow}private IntWritable result = new IntWritable();public IntSumReducer() {}public void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {int sum = 0;IntWritable val;context.getCounter(Counter.My_Reduce).increment(1L);for(Iterator i$ = values.iterator(); i$.hasNext(); sum += val.get()) {val = (IntWritable)i$.next();}this.result.set(sum);context.write(key, this.result);}}
总结
计数器是为了我们更好得统计数据和查找错误 是很有必要掌握清楚得一种手段
