MapReduce-计数器
计数器
计数器是收集作业统计信息的有效手段之一,用于质量控制或应用级统计。计数器还可辅助诊断系统故障。根据计数器值来记录某一特定事件的发生比分析一堆日志文件容易得多。
内置计数器
Hadoop为每个作业维护若干内置计数器,以描述多项指标。例如,某些计数器记录已处理的字节数和记录数,使用户可监控已处理的输入数据量和已产生的输出数据量。
这些计数器被分为若干个组,如下
1. 任务计数器
在任务执行过程中,任务计数器采集任务的相关信息,每个作业的所有任务的结果会被聚集起来。例如,MAP_INPUT_RECORDS计数器统计每个map任务输入记录的总数,并在一个作业的所有map任务上进行聚集,使得最终数字是整个作业的所有输入记录的总数。
任务计数器由关联任务维护,并定期发送给tasktracker(YARN中为nodemanager),再由tasktracker发送给jobtracker(YARN中为application master)。因此,计数器能够被全局地聚集。任务计数器的值每次都是完整传输的,而非自上次传输之后再继续尚未完成的传输,从而避免由于消息丢失而引发的错误。另外,如果一个任务在作业执行期间失败,则相关计数器的值会减小。
虽然只有当整个作业执行完之后计数器的值才是完整可靠的,但是部分计数器仍然可以再任务处理过程中提供一些有用的诊断信息,以便由Web界面监控。例如,PHYSICAL_MEMORY_BYTES\VIRTUAL_MEMORY_BYTES和COMMITED_HEAP_BYTES计数器显示特定任务执行过程中的内存使用变化情况。
内置的任务计数器包括在MapReduce任务计数器分组中的计数器以及在文件相关的计数器分组中。
内置的MapReduce任务计数器
内置的文件系统任务计数器
内置的FileInputFormat任务计数器
内置的FileOutputFormat任务计数器
2. 作业计数器
作业计数器由jobtracker(YARN中的application master)维护,因此无需再网络间传输数据,这一点与包括“用户定义的计数器”在内的其他计数器不同。这些计数器都是作业级别的统计量,其值不会随着任务运行而改变。例如,TOTAL_LAUNCHED_MAPS统计在作业执行过程中启动的map任务数,包括失败的map任务。
内置的作业计数器
用户自定义的Java计数器
计数器的值可在mapper或reducer中增加,计数器由一个Java枚举(enmu)类型来定义,以便对有关的计数器分组。一个作业可以定义的枚举类型数量不限,各个枚举类型所包含的字段数量也不限。枚举类型的名称即为组的名称,枚举类型的字段就是计数器名称。计数器是全局的。MapReduce框架将跨所有map和reduce聚集这些计数器,并在作业结束时产生一个最终结果。
示例:统计数据文件中空行条数
代码如下
package com.zhen.mapreduce.counter; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; /**
* @author FengZhen
* @date 2018年8月29日
* 计数器,统计输入文件中空白的行数
*/
public class SimpleCounterTest extends Configured implements Tool{ enum Empty{
EMPTY,
NOT_EMPTY
} static class SimpleCounterMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
String line = value.toString();
if (line.equals("")) {
context.getCounter(Empty.EMPTY).increment(1);
}else {
context.getCounter(Empty.NOT_EMPTY).increment(1);
}
context.write(value, new IntWritable(1));
}
} static class SimpleCounterReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
@Override
protected void reduce(Text key, Iterable<IntWritable> values,
Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable intWritable : values) {
sum += intWritable.get();
}
Counter empty = context.getCounter(Empty.EMPTY);
Counter not_empty = context.getCounter(Empty.NOT_EMPTY);
System.out.println("empty:"+empty.getValue() + "----not_empty:"+not_empty.getValue());
context.write(key, new IntWritable(sum));
}
} public int run(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJobName("SimpleCounterTest");
job.setJarByClass(SimpleCounterTest.class); job.setMapperClass(SimpleCounterMapper.class);
job.setReducerClass(SimpleCounterReducer.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); // job.setInputFormatClass(FileInputFormat.class);
// job.setOutputFormatClass(FileOutputFormat.class); FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); return job.waitForCompletion(true) ? 0 : 1;
} public static void main(String[] args) throws Exception {
String[] params = new String[]{"hdfs://fz/user/hdfs/MapReduce/data/counter/containsEmpty/input","hdfs://fz/user/hdfs/MapReduce/data/counter/containsEmpty/output"};
int exitCode = ToolRunner.run(new SimpleCounterTest(), params);
System.exit(exitCode);
} }
打jar包上传到服务器,执行
scp /Users/FengZhen/Desktop/Hadoop/file/SimpleCounter.jar root@192.168.1.124:/usr/local/test/mr
hadoop jar SimpleCounter.jar com.zhen.mapreduce.counter.SimpleCounterTest
结果如下
[root@HDP4 mr]# hadoop jar SimpleCounter.jar com.zhen.mapreduce.counter.SimpleCounterTest
18/09/08 20:15:44 INFO client.RMProxy: Connecting to ResourceManager at HDP4/192.168.1.124:8032
18/09/08 20:15:46 WARN mapreduce.JobResourceUploader: Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this.
18/09/08 20:15:47 INFO input.FileInputFormat: Total input paths to process : 1
18/09/08 20:15:47 INFO mapreduce.JobSubmitter: number of splits:1
18/09/08 20:15:48 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1535207597429_0006
18/09/08 20:15:51 INFO impl.YarnClientImpl: Submitted application application_1535207597429_0006
18/09/08 20:15:52 INFO mapreduce.Job: The url to track the job: http://HDP4:8088/proxy/application_1535207597429_0006/
18/09/08 20:15:52 INFO mapreduce.Job: Running job: job_1535207597429_0006
18/09/08 20:16:09 INFO mapreduce.Job: Job job_1535207597429_0006 running in uber mode : false
18/09/08 20:16:09 INFO mapreduce.Job: map 0% reduce 0%
18/09/08 20:16:22 INFO mapreduce.Job: map 100% reduce 0%
18/09/08 20:16:34 INFO mapreduce.Job: map 100% reduce 100%
18/09/08 20:16:34 INFO mapreduce.Job: Job job_1535207597429_0006 completed successfully
18/09/08 20:16:34 INFO mapreduce.Job: Counters: 51
File System Counters
FILE: Number of bytes read=78
FILE: Number of bytes written=298025
FILE: Number of read operations=0
FILE: Number of large read operations=0
FILE: Number of write operations=0
HDFS: Number of bytes read=174
HDFS: Number of bytes written=28
HDFS: Number of read operations=6
HDFS: Number of large read operations=0
HDFS: Number of write operations=2
Job Counters
Launched map tasks=1
Launched reduce tasks=1
Data-local map tasks=1
Total time spent by all maps in occupied slots (ms)=10609
Total time spent by all reduces in occupied slots (ms)=8008
Total time spent by all map tasks (ms)=10609
Total time spent by all reduce tasks (ms)=8008
Total vcore-milliseconds taken by all map tasks=10609
Total vcore-milliseconds taken by all reduce tasks=8008
Total megabyte-milliseconds taken by all map tasks=10863616
Total megabyte-milliseconds taken by all reduce tasks=8200192
Map-Reduce Framework
Map input records=9
Map output records=9
Map output bytes=70
Map output materialized bytes=74
Input split bytes=141
Combine input records=0
Combine output records=0
Reduce input groups=4
Reduce shuffle bytes=74
Reduce input records=9
Reduce output records=4
Spilled Records=18
Shuffled Maps =1
Failed Shuffles=0
Merged Map outputs=1
GC time elapsed (ms)=261
CPU time spent (ms)=5410
Physical memory (bytes) snapshot=499347456
Virtual memory (bytes) snapshot=5458706432
Total committed heap usage (bytes)=361893888
Shuffle Errors
BAD_ID=0
CONNECTION=0
IO_ERROR=0
WRONG_LENGTH=0
WRONG_MAP=0
WRONG_REDUCE=0
com.zhen.mapreduce.counter.SimpleCounterTest$Empty
EMPTY=4
NOT_EMPTY=5
File Input Format Counters
Bytes Read=33
File Output Format Counters
Bytes Written=28
可以看到EMPTY=4 NOT_EMPTY=5
MapReduce-计数器的更多相关文章
- MapReduce计数器
1.MapReduce计数器是什么? 计数器是用来记录job的执行进度和状态的.它的作用可以理解为日志.我们可以在程序的某个位置插入计数器,记录数据或者进度的变化情况. 2.MapReduce计数器能 ...
- 【原创】MapReduce计数器
MapReduce框架内置了一些计数器的支持,当然,我们也可以设置自己的计数器用来满足一些特殊的要求. 其实计数器可以用来完成很多事,关键要看你如何用,例如你想知道map输入数据的指定记录特定的信息有 ...
- MapReduce 计数器简介
转自:http://my.oschina.net/leejun2005/blog/276891?utm_source=tuicool&utm_medium=referral 1.计数器 简介 ...
- 大数据【四】MapReduce(单词计数;二次排序;计数器;join;分布式缓存)
前言: 根据前面的几篇博客学习,现在可以进行MapReduce学习了.本篇博客首先阐述了MapReduce的概念及使用原理,其次直接从五个实验中实践学习(单词计数,二次排序,计数器,join,分 ...
- Hadoop学习之路(十五)MapReduce的多Job串联和全局计数器
MapReduce 多 Job 串联 需求 一个稍复杂点的处理逻辑往往需要多个 MapReduce 程序串联处理,多 job 的串联可以借助 MapReduce 框架的 JobControl 实现 实 ...
- Hadoop MapReduce编程 API入门系列之计数器(二十七)
不多说,直接上代码. MapReduce 计数器是什么? 计数器是用来记录job的执行进度和状态的.它的作用可以理解为日志.我们可以在程序的某个位置插入计数器,记录数据或者进度的变化情况. Ma ...
- 用户定义的java计数器
mapreduce 计数器用来做某个信息的统计. 计数器是全局的.mapreduce 框架将跨所有map和reduce聚集这些计数器,并且作业结束时产生一个最终的结果. 语法像 java 的 enum ...
- MapReduce高级编程
MapReduce 计数器.最值: 计数器 数据集在进行MapReduce运算过程中,许多时候,用户希望了解待分析的数据的运行的运行情况.Hadoop内置的计数器功能收集作业的主要统计信息,可以帮助用 ...
- 大数据入门第九天——MapReduce详解(六)MR其他补充
一.自定义in/outputFormat 1.需求 现有一些原始日志需要做增强解析处理,流程: 1. 从原始日志文件中读取数据 2. 根据日志中的一个URL字段到外部知识库中获取信息增强到原始日志 3 ...
- Hadoop案例(四)倒排索引(多job串联)与全局计数器
一. 倒排索引(多job串联) 1. 需求分析 有大量的文本(文档.网页),需要建立搜索索引 xyg pingping xyg ss xyg ss a.txt xyg pingping xyg pin ...
随机推荐
- No image!使用border-color属性来制作小三角形
border属性在项目中使用的还是蛮频繁的.例如页签.按钮这样的. border简写属性是按照如下属性设置的: border:border-width/border-style/border-colo ...
- svn 更新文件冲突,提示中文乱码解决
问题描述: update 操作提示错误信息,中文乱码 和 “Please execute the 'Cleanup' command.” Cleanup 操作报错: 解决办法: 1. 工具下载(sql ...
- mysql 日期加减天数
MySQL 为日期增加一个时间间隔:date_add() now() //now函数为获取当前时间 select date_add(now(), interval 1 day); - 加1 ...
- Spec Explorer 工具学习
基础概念:http://blogs.msdn.com/b/sechina/archive/2009/12/28/test.aspx 在线教程:http://blogs.msdn.com/b/sechi ...
- 发挥inline-block作用
.pay-type { // 同行 display: inline-flex; padding: 0 @pay-type_2imgs_padding-width; } .pay-type_icon { ...
- Adjacency List
w Python Patterns - Implementing Graphs | Python.orghttps://www.python.org/doc/essays/graphs/ Graph ...
- Markov Process
w Markov Process -- from Wolfram MathWorld http://mathworld.wolfram.com/MarkovProcess.html 谷歌背后的数学_ ...
- JavaScript深入理解sort()方法
一. 基本用法 let arr1 = [3, 5, 7, 1, 8, 7, 10, 20, 19] console.log(arr1.sort()) // [1, 10, 19, 20, 3, 5, ...
- win7安装laravel
使用Packagist 镜像 建立一个composer.json文件,内容如下: { "name": "laravel/laravel", "desc ...
- Spring Boot在aop中获取request对象
doBefore(){ ServetRequestAttrbtes attributes = (ServetRequestAttrbtes)RequestContextHolder.getHttpat ...