MAPREDUCE求每年最高气温

首先建立一个input.txt,里面存储好老师给的数据。

然后在终端开启hadoop把它上传。

然后统计的代码如下:

package test;
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;
import java.io.IOException;
public class Temperature {
    static class TempMapper extends Mapper<LongWritable,Text,Text,IntWritable> {
        @Override
        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            System.out.print("Before Mapper:"+key+","+value);
            String line = value.toString();
            String year = line.substring(0,4);
            int temperature = Integer.parseInt(line.substring(8));
            context.write(new Text(year), new IntWritable(temperature));
            System.out.println("======"+ "After Mapper:"+new Text(year)+","+new IntWritable(temperature));
        }
    }
    static class TempReducer extends Reducer<Text,IntWritable,Text,IntWritable>{
        @Override
        public void reduce(Text key, Iterable<IntWritable> values,Context context)throws IOException,InterruptedException{
            int maxValue = Integer.MIN_VALUE;
            StringBuffer sb = new StringBuffer();
            for (IntWritable value : values){
                maxValue = Math.max(maxValue,value.get());
                sb.append(value).append(",");
            }
            System.out.print("Before Reduce: " +key+","+sb.toString());
            context.write(key,new IntWritable(maxValue));
            System.out.println("======"+ "After Reduce:"+key+","+maxValue);
        }
    }
    public static void main(String[] args) throws Exception {
        //输入路径
        String dst = "hdfs://localhost:8020/input.txt";
        //输出路径,必须是不存在的,空文件加也不行。
        String dstOut = "hdfs://localhost:8020/output";
        Configuration hadoopConfig = new Configuration();
        hadoopConfig.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
        hadoopConfig.set("fs.file.impl", org.apache.hadoop.fs.LocalFileSystem.class.getName());
        Job job = new Job(hadoopConfig);
        //如果需要打成jar运行,需要下面这句
        //job.setJarByClass(NewMaxTemperature.class);
        //job执行作业时输入和输出文件的路径
        FileInputFormat.addInputPath(job,new Path(dst));
        FileOutputFormat.setOutputPath(job,new Path(dstOut));
        //指定自定义的Mapper和Reducer作为两个阶段的任务处理类
        job.setMapperClass(TempMapper.class);
        job.setReducerClass(TempReducer.class);
        //设置最后输出结果的Key和Value的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        //执行job,直到完成
        job.waitForCompletion(true);
        System.out.println("Finished");
    }
}

  

posted @ 2022-11-04 08:06  Lindseyyip  阅读(41)  评论(0编辑  收藏  举报