HADOOP的学习笔记 (第五期) hadoop示例代码分析 .

本文详细解析了Hadoop WordCount程序的实现过程及代码细节,包括Mapper、Reducer的功能和配置方法,并介绍了combiner的作用及其应用场景。

上一期中已经能跑成功一个hadoop程序了。这一期来记录下,还分析下代码内容,我也只是参照。《Hadoop 权威指南》加上我自己的见解来进行分析。

示例代码:

  1. public class WordCount {  
  2.   
  3.     /** 
  4.      * extends Mapper<Object,Text,Text,IntWritable> 
  5.      * 其中此4个泛型的含义为k1,v1,k2,v2 
  6.      * 即:输入map的key与value, 
  7.      * map输出的key与value 
  8.      */  
  9.     public static class TokenizerMapper extends  
  10.             Mapper<Object, Text, Text, IntWritable> {  
  11.   
  12.         private final static IntWritable one = new IntWritable(1);  
  13.         private Text word = new Text();  
  14.   
  15.         public void map(Object key, Text value, Context context)  
  16.                 throws IOException, InterruptedException {  
  17.             StringTokenizer itr = new StringTokenizer(value.toString());  
  18.             while (itr.hasMoreTokens()) {  
  19.                 word.set(itr.nextToken());  
  20.                 context.write(word, one);  
  21.             }  
  22.         }  
  23.     }  
  24.   
  25.     /** 
  26.      * extends Reducer<Text,IntWritable,Text,IntWritable> 
  27.      * 其中此4个泛型的含义为k2,v2,k3,v3 
  28.      * 即:reduce输入的key与value 
  29.      * reduce输出的key与value 
  30.      * 其中此k2,v2与map中的k2,v2相同 
  31.      */  
  32.     public static class IntSumReducer extends  
  33.             Reducer<Text, IntWritable, Text, IntWritable> {  
  34.         private IntWritable result = new IntWritable();  
  35.   
  36.         public void reduce(Text key, Iterable<IntWritable> values, Context context)  
  37.                 throws IOException, InterruptedException {  
  38.             int sum = 0;  
  39.             for (IntWritable val : values) {  
  40.                 sum += val.get();  
  41.             }  
  42.             result.set(sum);  
  43.             context.write(key, result);  
  44.         }  
  45.     }  
  46.   
  47.     public static void main(String[] args) throws Exception {  
  48.         Configuration conf = new Configuration();  
  49.         conf.set("mapred.job.tracker""192.168.0.151:9001");  
  50.         String[] ars = new String[] { "input""output3" };  
  51.         String[] otherArgs = new GenericOptionsParser(conf, ars)  
  52.                 .getRemainingArgs();  
  53.         if (otherArgs.length != 2) {  
  54.             System.err.println("Usage: wordcount  ");  
  55.             System.exit(2);  
  56.         }  
  57.         //注册一个名为wordcount的job   
  58.         Job job = new Job(conf, "wordcount");  
  59.         //job以引用的类为WordCount   
  60.         job.setJarByClass(WordCount.class);  
  61.         //mapper引用的class   
  62.         job.setMapperClass(TokenizerMapper.class);  
  63.         //combiner引用的class   
  64.         job.setCombinerClass(IntSumReducer.class);  
  65.         //reduce引用的class   
  66.         job.setReducerClass(IntSumReducer.class);  
  67.         //k3输出的key   
  68.         job.setOutputKeyClass(Text.class);  
  69.         //v3输出的value   
  70.         job.setOutputValueClass(IntWritable.class);  
  71.         //数据输入路径   
  72.         FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
  73.         //数据输出路径   
  74.         FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));  
  75.         System.exit(job.waitForCompletion(true) ? 0 : 1);  
  76.     }  
  77. }  
public class WordCount {

	/**
	 * extends Mapper<Object,Text,Text,IntWritable>
	 * 其中此4个泛型的含义为k1,v1,k2,v2
	 * 即:输入map的key与value,
	 * map输出的key与value
	 */
	public static class TokenizerMapper extends
			Mapper<Object, Text, Text, IntWritable> {

		private final static IntWritable one = new IntWritable(1);
		private Text word = new Text();

		public void map(Object key, Text value, Context context)
				throws IOException, InterruptedException {
			StringTokenizer itr = new StringTokenizer(value.toString());
			while (itr.hasMoreTokens()) {
				word.set(itr.nextToken());
				context.write(word, one);
			}
		}
	}

	/**
	 * extends Reducer<Text,IntWritable,Text,IntWritable>
	 * 其中此4个泛型的含义为k2,v2,k3,v3
	 * 即:reduce输入的key与value
	 * reduce输出的key与value
	 * 其中此k2,v2与map中的k2,v2相同
	 */
	public static class IntSumReducer extends
			Reducer<Text, IntWritable, Text, IntWritable> {
		private IntWritable result = new IntWritable();

		public void reduce(Text key, Iterable<IntWritable> values, Context context)
				throws IOException, InterruptedException {
			int sum = 0;
			for (IntWritable val : values) {
				sum += val.get();
			}
			result.set(sum);
			context.write(key, result);
		}
	}

	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		conf.set("mapred.job.tracker", "192.168.0.151:9001");
		String[] ars = new String[] { "input", "output3" };
		String[] otherArgs = new GenericOptionsParser(conf, ars)
				.getRemainingArgs();
		if (otherArgs.length != 2) {
			System.err.println("Usage: wordcount  ");
			System.exit(2);
		}
		//注册一个名为wordcount的job
		Job job = new Job(conf, "wordcount");
		//job以引用的类为WordCount
		job.setJarByClass(WordCount.class);
		//mapper引用的class
		job.setMapperClass(TokenizerMapper.class);
		//combiner引用的class
		job.setCombinerClass(IntSumReducer.class);
		//reduce引用的class
		job.setReducerClass(IntSumReducer.class);
		//k3输出的key
		job.setOutputKeyClass(Text.class);
		//v3输出的value
		job.setOutputValueClass(IntWritable.class);
		//数据输入路径
		FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
		//数据输出路径
		FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}
}

此处我认为需要多解释的几个地方是:

1.关于job设置输入输出的数据类型,有几种配置类型的属性,以及必须类型一致的属性:

2.关于combiner合并函数:

为map执行完以后进行合并操作的一个函数,此函数的输出,与reduce的输入相同,此是一个优化方案,应该相当于reduce的一个前置,所以说,无论调用多少次combiner合并函数,最后的reduce结果应该都是不变的,那什么场景适合用合并函数呢?举个例子:找寻最大值

第一个map任务的输出结果为:

2000 50

2000 70

2000 80

第二个map任务的输出结果为:

2000 90

2000 99

reduce函数调用的时候输出的结果是{2000,{50,70,80,90,99}},99最大,最后输出的结果为(2000,99)

而combiner的操作结果就是。

传入reduce函数的数据为{2000,{80,99}}最后输出的结果为{2000,99}

最后的结果与原来一样,只是流程有些不相同。

即:max(50,70,80,90,99) = max(max(50,70,80),max(90,99))但是不是所有场景都是用,比如求平均值的时候。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值