计数器的简绍
  • 计数器是手机作业统计信息的有效手段之一,用于质量控制或应用统计。计数器还可辅助字段系统故障。计数器不但获取计数器值比较方便,害可以根据特定值统计发生的次数
hadoop的内置计数器
MapReduce任务计数器 org.apache.hadoop.mapreuce.TaskCounter
文件系统计数器 org.apache.hadoop.mapreuce.FileSystemCounter
FileInputFormat计数器 org.apache.hadoop.mapreuce.FileInputFormatCount
FileOutputFormat计数器 org.apache.hadoop.mapreuce.FileOutputFormatCounter
作业计数器 org.apache.hadoop.mapreuce.JobCounter
定义一个普通的计数器
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class counter01Map extends Mapper<LongWritable, Text, IntWritable, NullWritable> {
    IntWritable k = new IntWritable();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        int score = Integer.parseInt(value.toString());

        // 普通方法定义计数器
        if (score >= 60) {
            org.apache.hadoop.mapreduce.Counter counter1 = context.getCounter("及格", "成绩");
             counter1.increment(1);
        } else {
            org.apache.hadoop.mapreduce.Counter counter2 = context.getCounter("不及格", "成绩");
            counter2.increment(1);
        }
        k.set(score);
        context.write(k, NullWritable.get());
    }
  • 结果

MapReduce--->计数器_hadoop

枚举计数器
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class counter01Map extends Mapper<LongWritable, Text, IntWritable, NullWritable> {
    IntWritable k = new IntWritable();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        int score = Integer.parseInt(value.toString());
        if (score>=60){
            context.getCounter(Counter.及格).increment(1);
        }else {
            context.getCounter(Counter.不及格).increment(1);
        }
        k.set(score);
        context.write(k, NullWritable.get());
    }

    //使用枚举的方法定义计数器
    public static enum Counter {
        及格, 不及格;

        public void increment(int i) {
        }
    }
}