MapReduce의 대표적인 사례 ----------최고 온도 통계

수요: 정해진 날짜의 최고 온도를 구합니다


보류 중인 데이터 내용:
​ 201701082.6
​ 201701066
​ 2017020810
​ 2017030816.33
​ 2017060833.0
행당 상위 8위는 날짜, 8위에서 뒤로 온도

코드

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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;
/**
 *      
 * @author lyd
 * 
 *   :
 * 
 * 201701082.6
 * 201701066
 * 2017020810
 * 2017030816.33
 * 2017060833.0
 * 2017050126.6
 * 2017050320.9
 *
 */
public class HighTem {
	public static class MyMapper extends Mapper{
		@Override
		protected void map(Object key, Text value,Context context)
				throws IOException, InterruptedException {
			String line = value.toString();
			String tmp = line.substring(8, line.length());
			context.write(new Text(""), new Text(tmp));
			/**
			"" 201701082.6
			"" 201701066
			"" 2017020810
			"" 2017030816.33
			"" 2017060833.0
			"" 2017050126.6
			"" 2017050320.9
			 */
		}
	}
	
	/**
	 *    reducer 
	 * @author lyd
	 *
	 */
	public static class MyReducer extends Reducer{
		@Override
		protected void reduce(Text key, Iterable value,Context context)
				throws IOException, InterruptedException {
			/**
			 * "" list(2.6,6,10)
			 */
			double max = Double.MIN_VALUE;
			//     
			for (Text t : value) {
				if(max < Double.parseDouble(t.toString())){
					max = Double.parseDouble(t.toString());
				}
			}
			context.write(new Text(max+""), new Text(""));
			
		}
	}
	
	
	public static void main(String[] args) {
		try {
			//      
			Configuration conf = new Configuration();
			//  job
			Job job = new Job(conf, "HighTemp");
			// job      
			job.setJarByClass(HighTem.class);
			
			//  map     
			job.setMapperClass(MyMapper.class);
			FileInputFormat.addInputPath(job, new Path(args[0]));
			
			
			//  reduce     
			job.setReducerClass(MyReducer.class);
			job.setOutputKeyClass(Text.class);
			job.setOutputValueClass(Text.class);
			FileOutputFormat.setOutputPath(job, new Path(args[1]));
			
			//      job      
			int isok = job.waitForCompletion(true)?0:1;
			//  job
			System.exit(isok);
			
		} catch (IOException | ClassNotFoundException | InterruptedException e) {
			e.printStackTrace();
		}
	}
}

좋은 웹페이지 즐겨찾기