MapReduce 之Counter

一、Main代码大放送

        JobConf job = new JobConf(new Configuration(), HadoopWordCount.class);
        job.setJobName("word-count-job");
        //设置Mapper的输出的key和value的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        //输入和输出格式化
        job.setInputFormat(TextInputFormat.class);
        job.setOutputFormat(TextOutputFormat.class);
        job.setMapperClass(MyMapper.class);
        job.setReducerClass(MyReducer.class);
        //这是整个任务的的输出key、value
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        //格式化路径
        FileInputFormat.setInputPaths(job, new Path(this.inputPath));
        FileOutputFormat.setOutputPath(job, new Path("/lwj/" + new Random().nextInt() + "wordcount.txt"));
        job.setNumReduceTasks(1);
        JobClient client = new JobClient(jobConf);
        RunningJob runningJob = client.runJob(jobConf);
        if (runningJob.isComplete()) {
            Counters counters = runningJob.getCounters();
            LOG.info("Map Input Record : " + counters.getCounter(Task.Counter.MAP_INPUT_RECORDS));
            LOG.info("Map Output Record : " + counters.getCounter(Task.Counter.MAP_OUTPUT_RECORDS));
            LOG.info("Reduce Input Record : " + counters.getCounter(Task.Counter.REDUCE_INPUT_RECORDS));
            LOG.info("Reduce Output Record : " + counters.getCounter(Task.Counter.REDUCE_OUTPUT_RECORDS));
            LOG.info("Combine Input Record : " + counters.getCounter(Task.Counter.COMBINE_INPUT_RECORDS));
            LOG.info("Combine Output Record : " + counters.getCounter(Task.Counter.COMBINE_OUTPUT_RECORDS));

           Counters.Counter totalNumber = counters.findCounter("totalNumber", MyMapper.MyCounters.NUM_RECORDS.toString());
           LOG.info("================" + totalNumber.getValue());
        }

二、Map任务

public static class MyMapper implements Mapper<LongWritable, Text, Text, IntWritable> {
        static enum MyCounters {NUM_RECORDS}

        @Override
        public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
            //reporter.progress();
            //reporter.incrCounter(MyCounters.NUM_RECORDS, 1);
            reporter.incrCounter("totalNumber", MyCounters.NUM_RECORDS.name(), 1);
        }
    }


转载于:https://my.oschina.net/momisabuilder/blog/533619

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值