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);
		}
	}
	
}
        
                이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JAVA 객체 작성 및 제거 방법정적 공장 방법 정적 공장 방법의 장점 를 반환할 수 있습니다. 정적 공장 방법의 단점 류 공유되거나 보호된 구조기를 포함하지 않으면 이불류화할 수 없음 여러 개의 구조기 파라미터를 만났을 때 구축기를 고려해야 한다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.