hadoop 氣象資料分析

來源:互聯網
上載者:User
 學習hadoop幾天了,配置了三天終端,運行了幾個例子。現在自己寫一個程式,根據氣象資料,計算當年的最高氣溫。
類比美國氣象局發布的資料。資料格式為:0000001010 195005+ 00001+99999999(其中第一個紅色數字為年份,第二個數字為氣溫)。以1950為鍵,00001為值。
類比資料為:
0000001010195005+00001+99999999
0000001010195005+00200+99999999
0000001010195005-00040+99999999
0000001010195005-00030+99999999
0000001010195005+00500+99999999
0000001010195005+00303+99999999
0000001010195005-00435+99999999
0000001010195005+00342+99999999
0000001010195005+00034+99999999
0000001010195005-00023+99999999
0000001010195005+00034+99999999
0000001010195005+00234+99999999
0000001010195005-00034+99999999
0000001010195005+00022+99999999
0000001010195005+00940+99999999
0000001010195005-00034+99999999
0000001010195005+00244+99999999
0000001010195005-00034+99999999
0000001010195005-00022+99999999
0000001010195005+00090+99999999
0000001010195005-00240+99999999
0000001010195105+00001+99999999
0000001010195105+00200+99999999
0000001010195105-00040+99999999
0000001010195105-00030+99999999
0000001010195105+00500+99999999
0000001010195105+00303+99999999
0000001010195105-00435+99999999
0000001010195105+00342+99999999
0000001010195105+00034+99999999
0000001010195105-00023+99999999
0000001010195105+00034+99999999
0000001010195105+00234+99999999
0000001010195105-00034+99999999
0000001010195105+00022+99999999
0000001010195105+00940+99999999
0000001010195105-00034+99999999
0000001010195105+00244+99999999
0000001010195105-00034+99999999
0000001010195105-00022+99999999
0000001010195105+00090+99999999
0000001010195105-00240+99999999
其中1950年和1951年的資料是相同的,目的是為了測試兩年的最高氣溫的計算結果是否相同。

下面列舉代碼:

package climate;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 MaxTemeratureMapper extends Mapper<LongWritable, Text, Text, IntWritable>{    @Override    protected void map(LongWritable key, Text value, Context context)            throws IOException, InterruptedException {        String line = value.toString();               if(line.length() < 14){            System.out.println("less then 14");        }else{            String year = line.substring(10, 14);            Text tYear = new Text(year);    //                       int temperature =  Integer.parseInt(line.substring(17, 22));            IntWritable iTemperature = new IntWritable(temperature);                       context.write(tYear, iTemperature);        }    }}

package climate;import java.io.IOException;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Reducer;public class MaxTemperatureReduce extends Reducer<Text,IntWritable,Text,IntWritable>{    @Override    protected void reduce(Text key, Iterable<IntWritable> values,Context context)            throws IOException, InterruptedException {        int maxValue = Integer.MIN_VALUE;        IntWritable result = new IntWritable();               for(IntWritable var : values){            maxValue = Math.max(maxValue, var.get());        }        result.set(maxValue);        context.write(key, result);    }}

package climate;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;import org.apache.hadoop.util.GenericOptionsParser;public class Climate {       public static void main(String [] args) throws Exception{        Configuration conf = new Configuration();        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();               if (otherArgs.length != 2) {          System.err.println("Usage: climate in out directory");          System.exit(2);        }               long startTime = System.currentTimeMillis();               Job job = new Job(conf, "climate");        job.setJarByClass(Climate.class);        job.setMapperClass(MaxTemeratureMapper.class);        job.setCombinerClass(MaxTemperatureReduce.class);        job.setReducerClass(MaxTemperatureReduce.class);        job.setOutputKeyClass(Text.class);        job.setOutputValueClass(IntWritable.class);        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));               if(job.waitForCompletion(true))        {            System.out.println("dealing time:"+ (System.currentTimeMillis() - startTime) + "ms");        }               System.exit(job.waitForCompletion(true) ? 0 : 1);    }}

(3)運行結果
1950    940
1951    940


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.