Hadoop 의 - 사용자 정의 정렬 알고리즘 정렬 기능 구현

       :http://blog.csdn.net/l1028386804/article/details/46288107

먼저 첫 번 째 열 오름차 순 으로 배열 하고 첫 번 째 열 이 동시에 두 번 째 열 오름차 순 으로 배열 해 야 한다.긴 말 하지 않 고 바로 코드 를 올리다
1. Mapper 류 의 실현
	/**
	 * Mapper    
	 * @author liuyazhuang
	 *
	 */
	static class MyMapper extends Mapper{
		protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper.Context context) throws java.io.IOException ,InterruptedException {
			final String[] splited = value.toString().split("\t");
			final NewK2 k2 = new NewK2(Long.parseLong(splited[0]), Long.parseLong(splited[1]));
			final LongWritable v2 = new LongWritable(Long.parseLong(splited[1]));
			context.write(k2, v2);
		};
	}

2. Reducer 류 의 실현
	/**
	 * Reducer    
	 * @author liuyazhuang
	 *
	 */
	static class MyReducer extends Reducer{
		protected void reduce(NewK2 k2, java.lang.Iterable v2s, org.apache.hadoop.mapreduce.Reducer.Context context) throws java.io.IOException ,InterruptedException {
			context.write(new LongWritable(k2.first), new LongWritable(k2.second));
		};
	}

3. Writable Comparable 실현
/**
	 *  :       ?
	 *  :     v2      ,    k2 v2       ,    k2
	 * @author liuyazhuang
	 */
	static class  NewK2 implements WritableComparable{
		Long first;
		Long second;
		
		public NewK2(){}
		
		public NewK2(long first, long second){
			this.first = first;
			this.second = second;
		}
		
		
		@Override
		public void readFields(DataInput in) throws IOException {
			this.first = in.readLong();
			this.second = in.readLong();
		}

		@Override
		public void write(DataOutput out) throws IOException {
			out.writeLong(first);
			out.writeLong(second);
		}

		/**
		 *  k2     ,      .
		 *        ,  ;       ,     
		 * @author liuyazhuang
		 */
		@Override
		public int compareTo(NewK2 o) {
			final long minus = this.first - o.first;
			if(minus !=0){
				return (int)minus;
			}
			return (int)(this.second - o.second);
		}
		
		@Override
		public int hashCode() {
			return this.first.hashCode()+this.second.hashCode();
		}
		
		@Override
		public boolean equals(Object obj) {
			if(!(obj instanceof NewK2)){
				return false;
			}
			NewK2 oK2 = (NewK2)obj;
			return (this.first==oK2.first)&&(this.second==oK2.second);
		}
	}

4. 프로그램 입구 Main
	public static void main(String[] args) throws Exception{
		final Configuration configuration = new Configuration();
		
		final FileSystem fileSystem = FileSystem.get(new URI(INPUT_PATH), configuration);
		if(fileSystem.exists(new Path(OUT_PATH))){
			fileSystem.delete(new Path(OUT_PATH), true);
		}
		
		final Job job = new Job(configuration, SortApp.class.getSimpleName());
		
		//1.1         
		FileInputFormat.setInputPaths(job, INPUT_PATH);
		//              
		job.setInputFormatClass(TextInputFormat.class);
		
		//1.2      Mapper 
		job.setMapperClass(MyMapper.class);
		//       
		job.setMapOutputKeyClass(NewK2.class);
		job.setMapOutputValueClass(LongWritable.class);
		
		//1.3      
		job.setPartitionerClass(HashPartitioner.class);
		job.setNumReduceTasks(1);
		
		//1.4 TODO   、  
		
		//1.5  TODO (  )  
		
		//2.2       reduce 
		job.setReducerClass(MyReducer.class);
		//       
		job.setOutputKeyClass(LongWritable.class);
		job.setOutputValueClass(LongWritable.class);
		
		//2.3        
		FileOutputFormat.setOutputPath(job, new Path(OUT_PATH));
		//           
		job.setOutputFormatClass(TextOutputFormat.class);
		
		//      JobTracker  
		job.waitForCompletion(true);
	}

5. 전체 코드
package com.lyz.hadoop.sort;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.net.URI;

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.io.WritableComparable;
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;
import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;

/**
 * Hadoop    
 *            ,       ,       
 * @author liuyazhuang
 *
 */
public class SortApp {
	static final String INPUT_PATH = "hdfs://liuyazhuang:9000/input";
	static final String OUT_PATH = "hdfs://liuyazhuang:9000/out";
	public static void main(String[] args) throws Exception{
		final Configuration configuration = new Configuration();
		
		final FileSystem fileSystem = FileSystem.get(new URI(INPUT_PATH), configuration);
		if(fileSystem.exists(new Path(OUT_PATH))){
			fileSystem.delete(new Path(OUT_PATH), true);
		}
		
		final Job job = new Job(configuration, SortApp.class.getSimpleName());
		
		//1.1         
		FileInputFormat.setInputPaths(job, INPUT_PATH);
		//              
		job.setInputFormatClass(TextInputFormat.class);
		
		//1.2      Mapper 
		job.setMapperClass(MyMapper.class);
		//       
		job.setMapOutputKeyClass(NewK2.class);
		job.setMapOutputValueClass(LongWritable.class);
		
		//1.3      
		job.setPartitionerClass(HashPartitioner.class);
		job.setNumReduceTasks(1);
		
		//1.4 TODO   、  
		
		//1.5  TODO (  )  
		
		//2.2       reduce 
		job.setReducerClass(MyReducer.class);
		//       
		job.setOutputKeyClass(LongWritable.class);
		job.setOutputValueClass(LongWritable.class);
		
		//2.3        
		FileOutputFormat.setOutputPath(job, new Path(OUT_PATH));
		//           
		job.setOutputFormatClass(TextOutputFormat.class);
		
		//      JobTracker  
		job.waitForCompletion(true);
	}

	
	/**
	 * Mapper    
	 * @author liuyazhuang
	 *
	 */
	static class MyMapper extends Mapper{
		protected void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Mapper.Context context) throws java.io.IOException ,InterruptedException {
			final String[] splited = value.toString().split("\t");
			final NewK2 k2 = new NewK2(Long.parseLong(splited[0]), Long.parseLong(splited[1]));
			final LongWritable v2 = new LongWritable(Long.parseLong(splited[1]));
			context.write(k2, v2);
		};
	}
	
	/**
	 * Reducer    
	 * @author liuyazhuang
	 *
	 */
	static class MyReducer extends Reducer{
		protected void reduce(NewK2 k2, java.lang.Iterable v2s, org.apache.hadoop.mapreduce.Reducer.Context context) throws java.io.IOException ,InterruptedException {
			context.write(new LongWritable(k2.first), new LongWritable(k2.second));
		};
	}
	
	/**
	 *  :       ?
	 *  :     v2      ,    k2 v2       ,    k2
	 * @author liuyazhuang
	 */
	static class  NewK2 implements WritableComparable{
		Long first;
		Long second;
		
		public NewK2(){}
		
		public NewK2(long first, long second){
			this.first = first;
			this.second = second;
		}
		
		
		@Override
		public void readFields(DataInput in) throws IOException {
			this.first = in.readLong();
			this.second = in.readLong();
		}

		@Override
		public void write(DataOutput out) throws IOException {
			out.writeLong(first);
			out.writeLong(second);
		}

		/**
		 *  k2     ,      .
		 *        ,  ;       ,     
		 * @author liuyazhuang
		 */
		@Override
		public int compareTo(NewK2 o) {
			final long minus = this.first - o.first;
			if(minus !=0){
				return (int)minus;
			}
			return (int)(this.second - o.second);
		}
		
		@Override
		public int hashCode() {
			return this.first.hashCode()+this.second.hashCode();
		}
		
		@Override
		public boolean equals(Object obj) {
			if(!(obj instanceof NewK2)){
				return false;
			}
			NewK2 oK2 = (NewK2)obj;
			return (this.first==oK2.first)&&(this.second==oK2.second);
		}
	}
	
}

좋은 웹페이지 즐겨찾기