酱油HADOOP 1

 

有很多介绍Hadoop的资源,Hadoop自动的Doc文件夹中也包含很多学习文档,有中文版的。

http://www.cnblogs.com/wayne1017/archive/2007/03/18/668768.html

 关于MapReduce的内容,建议看看孟岩的这篇MapReduce:The Free Lunch Is Not Over!

 

import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

/** 
*  * 描述:WordCount explains by York  
* @author Hadoop Dev Group 
*/

public class WordCount {    
	/**    
	* Map的四个参数分别是Map输入的<key, value>类型和Map的输出 <key, value>类型
	* 建立Mapper类TokenizerMapper继承自泛型类Mapper     
	* Mapper类:实现了Map功能基类     
	* Mapper接口:     
	* WritableComparable接口:实现WritableComparable的类可以相互比较。所有被用作key的类应该实现此接口。     
	* Reporter 则可用于报告整个应用的运行进度,本例中未使用。      
	*      */ 
	
	public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{        
		/**        
		* IntWritable, Text 均是 Hadoop 中实现的用于封装 Java 数据类型的类,这些类实现了WritableComparable接口,         
		* 都能够被串行化从而便于在分布式环境中进行数据交换,你可以将它们分别视为int,String 的替代品。     
		* 声明one常量和word用于存放单词的变量         
		*/    
		private final static IntWritable one = new IntWritable(1);    
		private Text word = new Text();    
		
		/**         
		* Mapper中的map方法:         
		* void map(K1 key, V1 value, Context context)         
		* 映射一个单个的输入k/v对到一个中间的k/v对         
		* 输出对不需要和输入对是相同的类型,输入对可以映射到0个或多个输出对。         
		* Context:收集Mapper输出的<k,v>对。         
		* Context的write(k, v)方法:增加一个(k,v)对到context         
		* 程序员主要编写Map和Reduce函数.这个Map函数使用StringTokenizer函数对字符串进行分隔,通过write方法把单词存入word中     
		* write方法存入(单词,1)这样的二元组到context中     
		*/      
	
		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);
			}
		}
	}    
	 
	public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> {
		private IntWritable result = new IntWritable();    
		/**         
		 * Reducer类中的reduce方法:     
		 * void reduce(Text key, Iterable<IntWritable> values, Context context)         
		 * 中k/v来自于map函数中的context,可能经过了进一步处理(combiner),同样通过context输出                    
		 *  
		*/   
		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:map/reduce的j配置类,向hadoop框架描述map-reduce执行的工作         
		 * */    
		Configuration conf = new Configuration();
		String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
		if (otherArgs.length != 2) {
			System.err.println("Usage: wordcount <in> <out>");
			System.exit(2);
		}
		Job job = new Job(conf, "word count");    //设置一个用户定义的job名称    
		job.setJarByClass(WordCount.class); 
		job.setMapperClass(TokenizerMapper.class);    //为job设置Mapper类 
		job.setCombinerClass(IntSumReducer.class);    //为job设置Combiner类
		job.setReducerClass(IntSumReducer.class);    //为job设置Reducer类    
		job.setOutputKeyClass(Text.class);        //为job的输出数据设置Key类    
		job.setOutputValueClass(IntWritable.class);    //为job输出设置value类   
		FileInputFormat.addInputPath(job, new Path(otherArgs[0]));    //为job设置输入路径   
		FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//为job设置输出路径    
		System.exit(job.waitForCompletion(true) ? 0 : 1);        //运行job  
		}
	}
}


 

Map的结果,会通过partition分发到Reducer上,Reducer做完Reduce操作后,通过OutputFormat,进行输出,下面我们就来分析参与这个过程的类。

Mapper的结果,可能送到Combiner做合并,Combiner在系统中并没有自己的基类,而是用Reducer作为Combiner的基类,他们对外的功能是一样的,只是使用的位置和使用时的上下文不太一样而已。Mapper最终处理的键值对<key, value>,是需要送到Reducer去合并的,合并的时候,有相同key的键/值对会送到同一个Reducer那。哪个key到哪个Reducer的分配过程,是由Partitioner规定的。它只有一个方法,

getPartition(Text key, Text value, int numPartitions)

如果需要控制Map输出到那个Reduce上去执行,就需要重载getPartition,实现自己的partition。

输入是Map的结果对<key, value>和Reducer的数目,输出则是分配的Reducer(整数编号)。就是指定Mappr输出的键值对到哪一个reducer上去。系统缺省的Partitioner是HashPartitioner,它以key的Hash值对Reducer的数目取模,得到对应的Reducer。这样保证如果有相同的key值,肯定被分配到同一个reducre上。如果有N个reducer,编号就为0,1,2,3……(N-1)。

Reducer是所有用户定制Reducer类的基类,和Mapper类似,它也有setup,reduce,cleanup和run方法,其中setup和cleanup含义和Mapper相同,reduce是真正合并Mapper结果的地方,它的输入是key和这个key对应的所有value的一个迭代器,同时还包括Reducer的上下文。系统中定义了两个非常简单的Reducer,IntSumReducer和LongSumReducer,分别用于对整形/长整型的value求和。

下面是一个partition的示例。

http://blog.csdn.net/xw13106209/article/details/6912069

import java.io.IOException;
import java.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;

/**
 * 输入文本,以tab间隔 kaka 1 28 hua 0 26 chao 1 tao 1 22 mao 0 29 22
 * */

// Partitioner函数的使用

public class MyPartitioner {
	// Map函数
	public static class MyMap extends MapReduceBase implements
			Mapper<LongWritable, Text, Text, Text> {
		public void map(LongWritable key, Text value,
				OutputCollector<Text, Text> output, Reporter reporter)
				throws IOException {
			String[] arr_value = value.toString().split("\t");
			// 测试输出
			// for(int i=0;i<arr_value.length;i++)
			// {
			// System.out.print(arr_value[i]+"\t");
			// }
			// System.out.print(arr_value.length);
			// System.out.println();
			Text word1 = new Text();
			Text word2 = new Text();
			if (arr_value.length > 3) {
				word1.set("long");
				word2.set(value);
			} else if (arr_value.length < 3) {
				word1.set("short");
				word2.set(value);
			} else {
				word1.set("right");
				word2.set(value);
			}
			output.collect(word1, word2);
		}
	}

	public static class MyReduce extends MapReduceBase implements
			Reducer<Text, Text, Text, Text> {
		public void reduce(Text key, Iterator<Text> values,
				OutputCollector<Text, Text> output, Reporter reporter)
				throws IOException {
			int sum = 0;
			System.out.println(key);
			while (values.hasNext()) {
				output.collect(key, new Text(values.next().getBytes()));
			}
		}
	}

	// 接口Partitioner继承JobConfigurable,所以这里有两个override方法
	public static class MyPartitionerPar implements Partitioner<Text, Text> {
		/**
		 * getPartition()方法的 输入参数:键/值对<key,value>与reducer数量numPartitions
		 * 输出参数:分配的Reducer编号,这里是result
		 * */
		@Override
		public int getPartition(Text key, Text value, int numPartitions) {
			// TODO Auto-generated method stub
			int result = 0;
			System.out.println("numPartitions--" + numPartitions);
			if (key.toString().equals("long")) {
				result = 0 % numPartitions;
			} else if (key.toString().equals("short")) {
				result = 1 % numPartitions;
			} else if (key.toString().equals("right")) {
				result = 2 % numPartitions;
			}
			System.out.println("result--" + result);
			return result;
		}

		@Override
		public void configure(JobConf arg0) {
			// TODO Auto-generated method stub
		}
	}

	// 输入参数:/home/hadoop/input/PartitionerExample
	// /home/hadoop/output/Partitioner
	public static void main(String[] args) throws Exception {
		JobConf conf = new JobConf(MyPartitioner.class);
		conf.setJobName("MyPartitioner");
		// 控制reducer数量,因为要分3个区,所以这里设定了3个reducer
		conf.setNumReduceTasks(3);
		conf.setMapOutputKeyClass(Text.class);
		conf.setMapOutputValueClass(Text.class);
		// 设定分区类
		conf.setPartitionerClass(MyPartitionerPar.class);
		conf.setOutputKeyClass(Text.class);
		conf.setOutputValueClass(Text.class);
		// 设定mapper和reducer类
		conf.setMapperClass(MyMap.class);
		conf.setReducerClass(MyReduce.class);
		conf.setInputFormat(TextInputFormat.class);
		conf.setOutputFormat(TextOutputFormat.class);
		FileInputFormat.setInputPaths(conf, new Path(args[0]));
		FileOutputFormat.setOutputPath(conf, new Path(args[1]));
		JobClient.runJob(conf);
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值