您的位置:首页 > 其它

MR-5.MapReduce计数器介绍

2016-01-26 19:26 183 查看

计数器(couter)

在许多情况下,用户需要了解分析的数据,统计出输入记录数、统计数据无效记录数。通过计算器可以检测程序存储缺陷和数据集质量高低问题。计算器分成两类:内置计数器和自定义计算器。

内置计数器

Hadoop为每个作业维护提供若干内置计数器。以描述作业的指标。

l File System Counters

l Map-Reduce Framework

l Shuffle Errors

l File Input Format Counters

l File Output Format Counters

自定义计数器

MapReduce允许用户编写程序来定义计数器,计数器的值可以在mapper或者reducer中增加。MapReduce框架将跨所有的map和reduce聚集这些计数器,并在作业结束时产生一个最终结果。

案例分析:

(1)在MapReduce的mapper端计数器设定

protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

if (value.toString().contains("hello")) {

// 输入敏感词统计-当查询到hello关键字的词汇时,计数器加1

context.getCounter("Sensitive Counters", "hello").increment(1L);

}

String[] words = value.toString().split("\t");



for (String word : words) {

k2.set(word.getBytes());

v2.set(1);

context.write(k2, v2);

}

}

(2)在job端计数器获取

这里包括内置计数器的获取和用户自定义的计数器的获取。

Counters counters = job.getCounters();

//获取自定义的counter信息

Counter c1 = counters.findCounter("Sensitive Counters","hello");

System.out.println(c1.getValue());

//获取系统内置的counter信息

Counter c21 = counters.findCounter(TaskCounter.MAP_INPUT_RECORDS);

Counter c22 = counters.findCounter(TaskCounter.MAP_OUTPUT_RECORDS);

System.out.println(c21.getValue() + "-> " + c22.getValue());

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: