栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

【Hadoop学习项目】4. 求最大最小值

【Hadoop学习项目】4. 求最大最小值

0. 项目结构

1. MaxDriver
package hadoop_test.min_max_demo_04;

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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;



public class MaxDriver {

	public static void main(String[] args) throws Exception {

		System.setProperty("HADOOP_USER_NAME", "root");

		Configuration conf=new Configuration();
		Job job=Job.getInstance(conf);

		job.setJarByClass(MaxDriver.class);

		job.setMapperClass(MaxMapper.class);
		job.setReducerClass(MaxReducer.class);

		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);

		FileInputFormat.setInputPaths(job,new Path("/hadoop_test/min_max/min_max.data"));
		FileOutputFormat.setOutputPath(job,new Path("/hadoop_test/min_max/result"));
		
		job.waitForCompletion(true);

	}

}
2. MaxMapper
package hadoop_test.min_max_demo_04;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class MaxMapper extends Mapper{
	@Override
	protected void map(LongWritable key, Text value, Context context)
			throws IOException, InterruptedException {
		String line=value.toString();
//		拿出年份
		String year=line.substring(8,12);
//      拿出温度
		int temp=Integer.parseInt(line.substring(18));
		context.write(new Text(year), new IntWritable(temp));
	}

}
3. MaxReducer
package hadoop_test.min_max_demo_04;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class MaxReducer extends Reducer {
	
	@Override
	protected void reduce(Text key, Iterable values,
			Context context) throws IOException, InterruptedException {

		int max=0;						// 找最大的所以从0开始
		int min=Integer.MAX_VALUE;		// 找最小的所以从最大整数开始
		for(IntWritable value:values){
			if(maxvalue.get()){
				min=value.get();
			}
		}
		context.write(key, new Text(String.valueOf(max)+":"+String.valueOf(min)));
	}

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/729288.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号