MapReduce计数器详解

MapReduce全局程序计数器Counters 文章目录Counters定义以源码的TaskCounter计数器为例自定义Counter例子 Counters定义 Counters represent global counters, defined either by the MapReduce framework or applications. Each Counter can be of any Enum type. Counters of a particular Enum are bunched into groups of type Counters.Group. Applications can define arbitra 立即下载

计数器:计数器是用来记录job的执行进度和状态的。它的作用可以理解为日志。我们通常可以在程序的某个位置插入计数器,用来记录数据或者进度的变化情况,它比日志更便利进行分析。


1. 内置计数器

Hadoop其实内置了很多计数器,那么这些计数器在哪看呢?

我们先来看下最简单的wordcount程序。

HDFS上的源文件:

[hadoop@master logfile]$ hadoop fs -cat  /MR_Counter/diary
Today is 2016-3-22
I study mapreduce counter
I realized that mapreduce counter is simple

WordCount.java:

package com.oner.mr.mrcounter;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class WordCount {

	public static void main(String[] args) throws IOException,
			InterruptedException, URISyntaxException, ClassNotFoundException {
		Path inPath = new Path("/MR_Counter/");// 输入目录
		Path outPath = new Path("/MR_Counter/out");// 输出目录

		Configuration conf = new Configuration();
		// conf.set("fsdefaultFS", "hdfs://master:9000");
		FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf,
				"hadoop");
		if (fs.exists(outPath)) {// 如果输出目录已存在,则删除
			fs.delete(outPath, true);
		}

		Job job = Job.getInstance(conf);

		job.setJarByClass(WordCount.class);

		job.setMapperClass(MyMapper.class);
		job.setReducerClass(MyReducer.class);

		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(LongWritable.class);

		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(LongWritable.class);

		job.setInputFormatClass(TextInputFormat.class);
		job.setOutputFormatClass(TextOutputFormat.class);

		FileInputFormat.setInputPaths(job, inPath);
		FileOutputFormat.setOutputPath(job, outPath);

		job.waitForCompletion(true);
	}

	public static class MyMapper extends
			Mapper<LongWritable, Text, Text, LongWritable> {

		private static Text k = new Text();
		private static LongWritable v = new LongWritable(1);

		@Override
		protected void map(LongWritable key, Text value, Context context)
				throws IOException, InterruptedException {
			String line = value.toString();
			String[] words = line.split(" ");
			for (String word : words) {
				k.set(word);
				context.write(k, v);
			}
		}
	}

	public static class MyReducer extends
			Reducer<Text, LongWritable, Text, LongWritable> {

		private static LongWritable v = new LongWritable();

		@Override
		protected void reduce(Text key, Iterable<LongWritable> values,
				Reducer<Text, LongWritable, Text, LongWritable>.Context context)
				throws IOException, InterruptedException {
			int sum = 0;
			for (LongWritable value : values) {
				sum += value.get();
			}

			v.set(sum);

			context.write(key, v);
		}
	}

}


打成jar包后执行: hadoop jar wc.jar com.oner.mr.mrcounter.WordCount

发现有如下信息(注释部分是自己加的):

16/03/22 14:25:30 INFO mapreduce.Job: Counters: 49 // 表示本次job共49个计数器
	File System Counters // 文件系统计数器
		FILE: Number of bytes read=235
		FILE: Number of bytes written=230421
		FILE: Number of read operations=0
		FILE: Number of large read operations=0
		FILE: Number of write operations=0
		HDFS: Number of bytes read=189
		HDFS: Number of bytes written=86
		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 // 启动的map数为1
		Launched reduce tasks=1 // 启动的reduce数为1
		Data-local map tasks=1 
		Total time spent by all maps in occupied slots (ms)=12118
		Total time spent by all reduces in occupied slots (ms)=11691
		Total time spent by all map tasks (ms)=12118
		Total time spent by all reduce tasks (ms)=11691
		Total vcore-seconds taken by all map tasks=12118
		Total vcore-seconds taken by all reduce tasks=11691
		Total megabyte-seconds taken by all map tasks=12408832
		Total megabyte-seconds taken by all reduce tasks=11971584
	Map-Reduce Framework //MapReduce框架计数器
		Map input records=3
		Map output records=14
		Map output bytes=201
		Map output materialized bytes=235
		Input split bytes=100
		Combine input records=0
		Combine output records=0
		Reduce input groups=10
		Reduce shuffle bytes=235
		Reduce input records=14
		Reduce output records=10
		Spilled Records=28
		Shuffled Maps =1
		Failed Shuffles=0
		Merged Map outputs=1
		GC time elapsed (ms)=331
		CPU time spent (ms)=2820
		Physical memory (bytes) snapshot=306024448
		Virtual memory (bytes) snapshot=1690583040
		Total committed heap usage (bytes)=136122368
	Shuffle Errors // Shuffle错误计数器
		BAD_ID=0
		CONNECTION=0
		IO_ERROR=0
		WRONG_LENGTH=0
		WRONG_MAP=0
		WRONG_REDUCE=0
	File Input Format Counters // 文件输入格式计数器
		Bytes Read=89 // Map从HDFS上读取的字节数,共89个字节
	File Output Format Counters // 文件输出格式计数器
		Bytes Written=86 //Reduce输出到HDFS上的字节数,共86个字节

上面的信息就是内置计数器的一些信息,包括:

文件系统计数器(File System Counters)

作业计数器(Job Counters)

MapReduce框架计数器(Map-Reduce Framework)

Shuffle 错误计数器(Shuffle Errors)

文件输入格式计数器(File Output Format Counters)

文件输出格式计数器(File Input Format Counters)


2. 自定义计数器

Hadoop也支持自定义计数器,在Hadoop2.x中可以使用Context的getCounter()方法(其实是接口TaskAttemptContext的方法,Context继承了该接口)得到自定义计数器。

public Counter getCounter(Enum<?> counterName):Get the Counter for the given counterName

public Counter getCounter(String groupName, String counterName):Get the Counter for the given groupName and counterName

由此可见,可以通过枚举或者字符串来得到计数器。

计数器常见的方法有几下几个:

String getName():Get the name of the counter

String getDisplayName():Get the display name of the counter

long getValue():Get the current value

void setValue(long value):Set this counter by the given value

void increment(long incr):Increment this counter by the given value


假设现在要在控制台输出源文件中的一些敏感词的个数,这里设定“mapreduce”为敏感词,该如何做呢?

package com.oner.mr.mrcounter;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class WordCount {

	public static void main(String[] args) throws IOException,
			InterruptedException, URISyntaxException, ClassNotFoundException {
		Path inPath = new Path("/MR_Counter/");// 输入目录
		Path outPath = new Path("/MR_Counter/out");// 输出目录

		Configuration conf = new Configuration();
		// conf.set("fsdefaultFS", "hdfs://master:9000");
		FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf,
				"hadoop");
		if (fs.exists(outPath)) {// 如果输出目录已存在,则删除
			fs.delete(outPath, true);
		}

		Job job = Job.getInstance(conf);

		job.setJarByClass(WordCount.class);

		job.setMapperClass(MyMapper.class);
		job.setReducerClass(MyReducer.class);

		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(LongWritable.class);

		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(LongWritable.class);

		job.setInputFormatClass(TextInputFormat.class);
		job.setOutputFormatClass(TextOutputFormat.class);

		FileInputFormat.setInputPaths(job, inPath);
		FileOutputFormat.setOutputPath(job, outPath);

		job.waitForCompletion(true);

	}

	public static class MyMapper extends
			Mapper<LongWritable, Text, Text, LongWritable> {

		private static Text k = new Text();
		private static LongWritable v = new LongWritable(1);

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

			<span style="color:#FF0000;">Counter sensitiveCounter = context.getCounter("Sensitive Words:",
					"mapreduce");</span>// 创建一个组是Sensitive Words,名是mapreduce的计数器

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

			for (String word : words) {
				<span style="color:#FF0000;">if (word.equalsIgnoreCase("mapreduce")) {//如果出现了mapreduce,则计数器值加1
					sensitiveCounter.increment(1L);
				}</span>
				k.set(word);
				context.write(k, v);
			}
		}
	}

	public static class MyReducer extends
			Reducer<Text, LongWritable, Text, LongWritable> {

		private static LongWritable v = new LongWritable();

		@Override
		protected void reduce(Text key, Iterable<LongWritable> values,
				Reducer<Text, LongWritable, Text, LongWritable>.Context context)
				throws IOException, InterruptedException {
			int sum = 0;
			for (LongWritable value : values) {
				sum += value.get();
			}

			v.set(sum);

			context.write(key, v);
		}
	}

}

打成jar包后重新执行,发现控制台中确实多了一组计数器Sensitive Words:,其中有一个名叫mapreduce的计数器,值为2。



MATLAB随机生成m个三维坐标点,且各个坐标点之间的距离不小于n randi函数 randi(max,m,n); %生成均匀分布的随机整数,max生成的随机整数最大值,生成m行n列的矩阵 编写函数sampling function [x,y,z]=sampling(lowx,upx,lowy,upy,lowz,upz,m,n) %该函数用来产生不重复的随机整数矩阵 %low—随机整数下界;up—随机整数上界;m—坐标点个数;n—各个坐标点之间的最小距离 %编... 阅读详情

相关推荐

拆解一个800G光模块:手把手带你认识里面的MT插芯和光纤阵列(FA)

本文详细拆解了800G光模块中的关键组件MT插芯和光纤阵列(FA),揭示了并行光学技术如何实现高速数据传输。通过显微镜下的精密观察,展示了MT插芯的高精度构造和光纤阵列的连接奥秘,同时解析了高精度研磨工艺对光信号传输的重要性。这些微型组件是高速收发模块实现800Gbps带宽的核心技术。

weixin_30839881的博客 1541

MapReduce 计数器

1. MapReduce 计数器 1.1 计数器是什么 计数器一般用来记录 job 执行进度和状态信息。 如果自己手动实现计数,需要考虑将多个线程的计算结果合并,编码过于麻烦,通常使用 MapReduce 的计数器。 实际应用中可以用来统计任务的某个环节的执行次数或者数据量,并作为优化前后的参数依据。 1.2 计数器分类 ???? 内置计数器: 内置计数器用来描述该作业的各项指标。 ???? 自定义计数器: 用户可以通过自己定义计数器,来实现特定的需求,可以通过枚举的方式定义计数器,也可以通过 contex

weixin_47243236的博客 1119

【DAY11】超参数调整专题1

核心优势: 它不是随机选择下一个点,而是根据先前评估的结果建立一个概率模型(通常是高斯过程),预测哪些参数组合可能产生更好的结果,并据此选择下一个评估点。这使得它在寻找最优解方面通常比随机搜索更高效(用更少的迭代次数达到相似或更好的性能),特别是当模型训练(单次评估)非常耗时的时候。实际上,有着非常多的方式可以实现贝叶斯优化,上面的代码比较简洁美观,贝叶斯优化和网格搜索的代码书写风格高度一致。- 需要定义参数的搜索空间,与随机搜索类似,当搜索空间非常大时,它通常比网格搜索和随机搜索更有效。

daomingwu1的博客 747

Hadoop生态圈(二十四)- MapReduce Counter计数器

MapReduce计数器概述,MapReduce内置计数器,MapReduce自定义计数器,Map-Reduce Framework Counters,File System Counters Counters,Job Counters,File Input | Output Format Counters,MapReduce自定义计数器案例

程序园@大Null 1690

用VINS-Fusion跑通EuRoC数据集:Ubuntu22.04+ROS2环境下的保姆级评测指南

本文详细介绍了在Ubuntu22.04和ROS2 Humble环境下使用VINS-Fusion处理EuRoC数据集的完整流程。从环境配置、数据集转换到参数调优和性能评估,提供了一套全面的多传感器融合分析方案,帮助开发者在无人机精准定位场景中实现高效部署与优化。

weixin_29212867的博客 517

INCA基础教程

以软件自带Demo为例记录INCA学习过程,主要内容包括测量与标定。

yunqianrui的博客 6万+

MapReduce高级篇——全局计数器

在运行程序的时候,程序员很希望去了解程序的运行情况,MapReduce程序运行也是如此,此外MapReduce这种大数据运算,计算时间比较长,不提供一些信息的话,用户只能一边又一遍的调试来查找问题。为此MapReduce提供了计数器,在默认情况下,我们可以可以根据内置的计数器来了解大致情况,但是业务总不可能全都是相同的,有时候也需要自定义,MapReduce也为我们提供了相应的接口,本文主要讲解计数器该如何使用、自定义

wxk的博客 1614

银河麒麟系统下VSCode安装全攻略:从下载到桌面快捷方式(附常见问题解决)

本文详细介绍了在银河麒麟系统下安装和优化VSCode的全流程,包括环境准备、安装策略、权限管理、桌面快捷方式创建以及系统级性能调优。特别针对国产系统环境提供了常见问题解决方案和必备插件推荐,帮助开发者高效配置开发环境。

v6b7n8m9q0的博客 1133

MapReduce:默认Counter的含义

但reduce在执行前,它的输入数据是经过shuffle的merge后存储在reduce端本地磁盘中,所以这个数据就是所有reduce的总输入字节数。与map task 的split相关的数据都会保存于HDFS中,而在保存时元数据也相应地存储着数据是以怎样的压缩方式放入的,它的具体类型是什么,这些额外的数据是MapReduce框架加入的,与job无关,这里记录的大小就是表示额外信息的字节大小。map端的那个中间结果是有压缩好的有格式数据,所有它有两个length信息:源数据大小与压缩后数据大小。

qq_54990658的博客 129

UG/NX二次开发Siemens官方NXOPEN实例解析—1.3 BlockStyler/ExtrudewithPreview

随着工业智能化的不断发展,UG二次开发的需求越来越多,也吸引了大批的二开从业人员,本人作为一名资深IT从业者(10年+)也毅然加入二次开发大军。然而,和流行IT行业(互联网、金融、医疗等)相比,工业智能化的门槛显得更高一点,专业的工业软件,相对封闭的开发理念和更小的开发圈子,让刚进入二开的从业者有点举步维艰。边学边整理,希望通过这系列文章的整理能给二开的生态增添一叶绿。

MarcoPro的博客 1632

第39步 深度学习图像识别:Inception V3建模(Tensorflow)

第39步 深度学习图像识别:Inception V3建模(Tensorflow)

Jet4505的博客 3167

MapReduce计数器--详解

1、MapReduce计数器是什么?   计数器是用来记录job的执行进度和状态的。它的作用可以理解为日志。我们可以在程序的某个位置插入计数器,记录数据或者进度的变化情况。 2、MapReduce计数器能做什么?   MapReduce 计数器(Counter)为我们提供一个窗口,用于观察 MapReduce Job 运行期的各种细节数据。对MapReduce性能调优很有帮助,MapRedu...

S_Running_snail的博客 735

手把手教你完成基于cnn的手写数字识别系统MATLAB语言

手写数字识别是经典的 CNN 分类应用之一,常用的数据集就是 MNIST 手写数字数据集,包含 0~9 这 10 个数字的手写图片,每个数字都由 6 万幅训练图像和 1 万幅测试图像构成。为了便于直观分析,这里选择 MATLAB 工具箱提供的 DigitDataset 进行训练识别,读者也可以直接利用 MNIST 数据集或者自己设计的数据集进行实验。其中,数据集文件夹 db 如图 所示。在数据集文件夹 db 里面对每个数字都建立了子文件夹,各自包含 1000 幅 28×28 大小的二维灰度图像。

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 1368

Hadoop--MapReduce计数器详解

计数器输出 运行完毕作业之后的计数器输出 内置计数器 hadoop为每个作业提供了若干内置计数器,用以描述各项指标。 文件系统计数器 所属类:org.apache.hadoop.mapreduce.FileSystemCounter BYTES_READ:文件系统读取的字节数 BYTES_WRITTEN:文件系统写的字节数 FileInputFormat任务计数器 所属类:or...

super-yong的博客 974

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

MapReduce--3--常用计数器详解

在本帖中,我想解释Hadoop计数器的含义(在作业完成后通常可以看到的那些计数器)。我一直在分析我们相对较小的集群中长时间运行的作业的匮乏,Hadoop计数器在这个调查中极其重要。不幸的是,我找不到任何能详细解释这些含义的资源。在下面的表格中,我试图清楚地描述Hadoop 2.6版本中每个计数器的含义。 信息来源: https://www.mapr.com/blog/management-....

谦卑t 275

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

MapReduce计数器

1、MapReduce计数器是什么? 计数器是用来记录job的执行进度和状态的。它的作用可以理解为日志。我们可以在程序的某个位置插入计数器,记录数据或者进度的变化情况。 2、MapReduce计数器能做什么? MapReduce 计数器(Counter)为我们提供一个窗口,用于观察 MapReduce Job 运行期的各种细节数据。对MapReduce性能调优很有帮助,Ma...

363

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

MapReduce计数器完全指南:任务监控的“仪表盘“

*计数器(Counter)**是MapReduce框架提供的一种轻量级统计工具,用于收集作业运行时的各种统计信息。它们以键值对的形式存在,可以在作业执行过程中动态更新,最终在作业完成后汇总展示。// 计数器的基本形式计数器名称:Map输入记录数当前值:1,234,567// 定义计数器枚举VALID_RECORDS, // 有效记录数INVALID_RECORDS, // 无效记录数MISSING_FIELDS, // 缺失字段记录数DUPLICATE_RECORDS // 重复记录数。

✨ 欢迎来到【Seal ^_^ 的优快云博客】!✨ 1779

Hadoop-Mapreduce

MapReduce介绍: MapReduce思想在生活中处处可见,或多或少都曾接触过这种思想。Mapreduce的思想核心是分而治之,使用于大量复杂 的任务处理场景(大规模数据处理场景)。 Map复杂分,即把复杂的任务分解为若干个简单的任务,来并行处理。可以进行拆分的前提是这些小任务可以并行计算,彼此 间几乎没有依赖关系。 Reduce负责合,即对ma...

散步的博客 301

12、MapReduce编程案例

文件开发环境搭建请参考《11、HDFS编程案例》一文 一、MapReduce项目开发说明 关于MapReduc程序最核心的操作类就是Job接口类,可以知道linux下当我们通过hadoop提交jar包运行的时候,控制台打印的就是job运行整个过程,包括Map完成百分比Reduce完成百分比等。所以Job的核心就主要包括Map数据输入读取过程与Reduce数据的计算输出过程。当然中间也包括一个shu...

lixiang987654321的专栏 1078

MapReduce当中的计数器

MapReduce当中的计数器 计数器是收集作业统计信息的有效手段之一,用于质量控制或应用级统计 计数器还可辅助诊断系统故障。 如果需要将日志信息传输到map 或reduce 任务, 更好的方法通常是看能否用一个计数器值来记录某一特定事件的发生。 对于大型分布式作业而言,使用计数器更为方便。除了因为获取计数器值比输出日志更方便,还有根据计数器值统计特定事件的发生次数要比分析一堆日志文件容易得多。 ...

6028

mapreduce的组件介绍

第一部分:重要的组件 Combiner •什么是Combiner •combine函数把一个map函数产生的<key,value>对(多个key, value)合并成一个新的<key2,value2>. 将新的<key2,value2>作为输入到reduce函数中,其格式与reduce函数相同。 •这样可以有效的较少中间结果,减少网络传输负荷...

weixin_30437481的博客 315

mapreduce中使用计数器

自定义计数器:context.getCounter(a,b).increment(1)

独孤小乐子的博客 729
上一篇: MapReduce on HBase
下一篇: MapReduce实现二阶矩阵相乘
Wang_AI
博客等级 码龄12年 988粉丝 162原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值