MapReduce--2--MapReduce全局计数器

MapReduce的全局计数器


1.1、介绍

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

 

MapReduce 计数器(Counter)为我们提供一个窗口,用于观察 MapReduce Job 运行期的各种细节数据。对MapReduce性能调优很有帮助,MapReduce性能优化的评估大部分都是基于这些 Counter 的数值表现出来的。

 

MapReduce 自带了许多默认Counter,现在我们来分析这些默认Counter 的含义,方便大家观察 Job 结果,如输入的字节数、输出的字节数、Map端输入/输出的字节数和条数、Reduce端的输入/输出的字节数和条数等


1.2、需求

在实际生产代码中,常常需要将数据处理过程中遇到的不合规数据行进行全局计数,类似这种需求可以借助MapReduce框架中提供的全局计数器来实现


1.3、场景介绍

现在我们通过全局计数器来实现,统计一个目录下的所有文件或者一个文件中的总行数和总单词数



1.4、代码实现


package com.ghgj.mazh.mapreduce.counter;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordAndLineCounter {

	/**
	 * mapreduce全局计数器,是用枚举类进行计数器的定义
	 * COUNT_LINES用来统计总行数, 
	 * COUNT_WORDS用来统计总单词数
	 */
	enum MyCouterWordCount {
		COUNT_WORDS, COUNT_LINES
	}

	// 驱动程序
	public static void main(String[] args) throws Exception {

		// 指定hdfs相关的参数
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);
		// 设置jar包所在路径
		job.setJarByClass(WordAndLineCounter.class);

		job.setMapperClass(WordAndLineCounterMapper.class);

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

		// 本地路径
		Path inputPath = new Path("D:\\bigdata\\wordcount\\input");
		FileInputFormat.setInputPaths(job, inputPath);

		Path outputPath = new Path("D:\\bigdata\\wordcount\\output");
		FileSystem fs = FileSystem.get(conf);
		if (fs.exists(outputPath)) {
			fs.delete(outputPath, true);
		}
		FileOutputFormat.setOutputPath(job, outputPath);

		// 最后提交任务
		boolean waitForCompletion = job.waitForCompletion(true);
		System.exit(waitForCompletion ? 0 : 1);
	}

	private static class WordAndLineCounterMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
		@Override
		protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

			// 获取计数器,行数计数器
			Counter counter = context.getCounter(MyCouterWordCount.COUNT_LINES);
			// 计数器计数
			counter.increment(1L);

			String[] words = value.toString().split(" ");
			for (String word : words) {
				if(!StringUtils.isBlank(word)){
					context.getCounter(MyCouterWordCount.COUNT_WORDS).increment(1L);
				}
			}
			
		}
	}
}



1.5、输入数据


hello huangbo
hello xuzheng
hello wangbaoqiang
one two three four five
one two three four
one two three
one two
hello hi

1.6、计算结果

 INFO [main] - Job job_local1030848363_0001 completed successfully
 INFO [main] - Counters: 35
	File System Counters
		FILE: Number of bytes read=614
		FILE: Number of bytes written=506970
		FILE: Number of read operations=0
		FILE: Number of large read operations=0
		FILE: Number of write operations=0
	Map-Reduce Framework
		Map input records=8
		Map output records=0
		Map output bytes=0
		Map output materialized bytes=6
		Input split bytes=104
		Combine input records=0
		Combine output records=0
		Reduce input groups=0
		Reduce shuffle bytes=6
		Reduce input records=0
		Reduce output records=0
		Spilled Records=0
		Shuffled Maps =1
		Failed Shuffles=0
		Merged Map outputs=1
		GC time elapsed (ms)=0
		CPU time spent (ms)=0
		Physical memory (bytes) snapshot=0
		Virtual memory (bytes) snapshot=0
		Total committed heap usage (bytes)=514850816
	Shuffle Errors
		BAD_ID=0
		CONNECTION=0
		IO_ERROR=0
		WRONG_LENGTH=0
		WRONG_MAP=0
		WRONG_REDUCE=0
	com.ghgj.mazh.mapreduce.counter.WordAndLineCounter$MyCouterWordCount
		COUNT_LINES=8
		COUNT_WORDS=22
	File Input Format Counters 
		Bytes Read=127
	File Output Format Counters 
		Bytes Written=8


这就是最后的计算结果:在上面结果中的倒数第5-7行

com.ghgj.mazh.mapreduce.counter.WordAndLineCounter$MyCouterWordCount
		COUNT_LINES=8
		COUNT_WORDS=22


各位看官,有没有看懂的,如果有疑问的地方,可以举手提问。


  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
hadoop-mapreduce-client-core是Hadoop分布式计算框架中的核心模块之一。它主要包含了Hadoop MapReduce的核心功能和API接口,是实现MapReduce编程模型的必备组件。 Hadoop MapReduce是一种用于大规模数据处理的编程模型,其核心思想是将大规模数据集分解成多个较小的数据块,分别在集群中的不同机器上进行处理,最后将结果整合。hadoop-mapreduce-client-core模块提供了与MapReduce相关的类和方法,方便开发者实现自定义的Map和Reduce任务。 具体来说,hadoop-mapreduce-client-core模块包含了以下重要组件和功能: 1. Job:Job表示一个MapReduce任务的定义和描述,包括输入路径、输出路径、Mapper和Reducer等。 2. Mapper:Mapper是MapReduce任务中的映射函数,它负责将输入数据转换成<key, value>键值对的形式。 3. Reducer:Reducer是MapReduce任务中的归约函数,它按照相同的key将所有Mapper输出的value进行聚合处理。 4. InputFormat:InputFormat负责将输入数据切分成多个InputSplit,每个InputSplit由一个Mapper负责处理。 5. OutputFormat:OutputFormat负责将Reducer的输出结果写入指定的输出路径中。 使用hadoop-mapreduce-client-core模块,开发者可以基于Hadoop分布式计算框架快速开发并行处理大规模数据的应用程序。通过编写自定义的Mapper和Reducer,可以实现各种类型的分布式计算,如数据清洗、聚合分析、机器学习等。 总之,hadoop-mapreduce-client-core是Hadoop分布式计算框架中的核心模块,提供了实现MapReduce编程模型所需的基本功能和API接口。使用该模块,开发者可以利用Hadoop的分布式计算能力,高效地处理和分析大规模数据。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值