2019-9-4 [MapReduce] run:获取counter信息并输出

Counters设置在job的后面

//执行job
        count = job.waitForCompletion(true)?0:-1;
        //counter是job执行后的统计信息
        Counters cs = job.getCounters();
        System.out.println("Counter count"+cs.countCounters());
        for (CounterGroup cg : cs) {
            System.out.println("\t"+cg.getDisplayName());
            for (Counter counter : cg) {
                System.out.println("\t\t"+counter.getDisplayName()+"="+counter.getValue());
            }
        }
        //counter ==>json==>mysql==>报表
        //返回
        return count;       

完整代码:

/**
 * MyCounterTest.java
 * com.hnxy.mr
 * Copyright (c) 2019, 子墨版权所有.
 * @author   ZIMO
 * @Date     2019年9月4日   
 */
public class MyCounterTest extends Configured implements Tool{
    
    /**
     * MAP CLASS
     * @author   ZIMO
     * @Date     2019年9月4日
     */
    private static class MyMapper extends Mapper<LongWritable, Text, Text,IntWritable > {
        
        //定义map的变量
        private Text outkey = new Text();
        private IntWritable outval = new IntWritable(1);
        private String[] strs = null;
        
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
                throws IOException, InterruptedException {
            strs = value.toString().split(" ");
            //记录counter
            context.getCounter("数据行信息","总行数").increment(1);
            //业务判断
            if (null != strs && strs.length == 3){
                //正确数据
                context.getCounter("数据行信息", "有效行数").increment(1);
                    //1 找 问
                    outkey.set(strs[2]);//河北省
                    context.write(outkey, outval);//1
                
            }else {
                context.getCounter("数据行信息", "无效行数").increment(1);
            }
        }   
    }
    
    /**
     * REDUCE CLASS
     * @author   ZIMO
     * @Date     2019年9月4日
     */
    private static class MyReduce extends Reducer<Text,IntWritable, Text, LongWritable> {
        
        //定义reduce
        private LongWritable outval = new LongWritable();
        private Long tmp = 0L;
​
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values,
                Reducer<Text, IntWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
            //清空上一次key的累加记录
            tmp = 0L;
            for (IntWritable i : values) {
                tmp += i.get();
            }
            //保存此次的数据
            outval.set(tmp);
            context.write(key, outval);                 
        }
    }
    
    /**
     * JOB RUNNER METHOD
     * @author ZIMO
     * @see org.apache.hadoop.util.Tool#run(java.lang.String[])
     */
    public int run(String[] args) throws Exception {
        //创建方法的返回值
        int count = -1;
        //创建hadoop 配置文件加载对象
        Configuration conf = this.getConf();
        //创建本次任务
        Job job = Job.getInstance(conf, "counter_test");
        //配置job
        //设置打包类
        job.setJarByClass(MyCounterTest.class);
        //整理一下job输入和输出
        Path in = new Path(args[0]);
        Path out = new Path(args[1]);
        //输入目录必须要有 输出目录必须没有
        FileSystem fs = FileSystem.get(conf);
        if (fs.exists(out)) {
            //hadoop fs -rm -r
            fs.delete(out,true);
            System.out.println("The Old OutPutPath is deleted!");
        }
        //设置MR类
        job.setMapperClass(MyMapper.class);
        job.setReducerClass(MyReduce.class);
        //设置MR输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        //设置数据格式化
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        //设置输入和输出路径
        FileInputFormat.addInputPath(job, in);
        FileOutputFormat.setOutputPath(job, out);
        //执行job
        count = job.waitForCompletion(true)?0:-1;
        //counter是job执行后的统计信息
        Counters cs = job.getCounters();
        System.out.println("Counter count"+cs.countCounters());
        for (CounterGroup cg : cs) {
            System.out.println("\t"+cg.getDisplayName());
            for (Counter counter : cg) {
                System.out.println("\t\t"+counter.getDisplayName()+"="+counter.getValue());
            }
        }
        //counter ==>json==>mysql==>报表
        //返回
        return count;       
    }
    
    /**
     * PROGRAM MAIN METHOD 
     * @param args VIP
     */
    public static void main(String[] args) {
        try {
            int result = ToolRunner.run(new MyCounterTest(), args);
            String msg = result==0?"JOB OK!":"JOB FAIL!";
            System.out.println(msg);
            System.exit(result);
        } catch (Exception e) {
            e.printStackTrace();
            
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值