MapReduce數據分析(4)最大值

四:MapReduce第四講:Max(最大值)

最大值的話小編沒有什麼可以說的,我相信大家都明白,這次我就直接上案列和代碼了。

數據如下:
在這裏插入圖片描述
案列:編寫MapReduce代碼獲得每年的最高氣溫。

代碼:

package demo;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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;
//2014010114

public class MaxTmp {
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		Configuration conf=new Configuration();
		Job job = Job.getInstance(conf);
		job.setJarByClass(demo.class);
		job.setMapperClass(MMapper.class);
		job.setReducerClass(MReduce.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(LongWritable.class);
		FileInputFormat.setInputPaths(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		job.waitForCompletion(true);


	}
	
	public static class MMapper extends Mapper<LongWritable, Text, Text, IntWritable>{


		protected void map(LongWritable key, Text value, Context context)
				throws IOException, InterruptedException {
			//轉換數據類型
			String line = value.toString();
			//截取數據中的0-6位。
			String year = line.substring(0,6);
			//轉換int類型並截取數據中的8-10位
			int tmp = Integer.parseInt(line.substring(8,10));
			//直接寫入
			context.write(new Text(year), new IntWritable(tmp));
			//201401    14
		}

	}
	public static class MReduce extends Reducer<Text, IntWritable, Text, IntWritable>{
		private IntWritable v = new IntWritable();
		
		protected void reduce(Text key, Iterable<IntWritable> value,
				Context context) throws IOException, InterruptedException {
			//			 14
	
			int maxv = Integer.MIN_VALUE;
			
			for (IntWritable v : value) {
			
				maxv=Math.max(maxv, v.get());
			
			}
			v.set(maxv);
		
			context.write(key, v);
			
		}

	}


}

運行結果:
在這裏插入圖片描述
下面我附上全部數據:
2014010114
2014010216
2014010317
2014010410
2014010506
2012010609
2012010732
2012010812
2012010919
2012011023
2001010116
2001010212
2001010310
2001010411
2001010529
2013010619
2013010722
2013010812
2013010929
2013011023
2008010105
2008010216
2008010337
2008010414
2008010516
2007010619
2007010712
2007010812
2007010999
2007011023
2010010114
2010010216
2010010317
2010010410
2010010506
2015010649
2015010722
2015010812
2015010999
2015011023

本次教程就到此結束了,博主會把關於MapReduce數據分析的各種操作講解給大家聽。有什麼不懂的下方評論,博主看到都會去解答的。還有不要忘了多多支持博主呦,你們的支持就是我的動力。

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章