Hadoop-02-第一个MapReduce程序--统计每年最高温度

来源:互联网 发布:php 取出字符串后几位 编辑:程序博客网 时间:2024/04/30 17:32

第一个MapReduce程序--根据气象数据集求出每年的最高气温

 

使用Eclipse开发MapReduce程序的步骤:

1.ubuntu上安装eclipse

2.关闭eclipse,将hadoop安装目录下contrib文件夹下的eclipse-plugin文件夹下的hadoop-0.20.2-eclipse-plugin复制到eclipse安装目录下的plugins目录下。重启eclipse

3.eclipse中点击Window-->preferences-->Hadoop Map/Reduce,在右侧Hadoop Installation Directory中选择自己hadoop的安装目录。

4.eclipse中点击Windows-->show view-->Map/Reduce locations,显示该试图。

5.Map/Reduce Locations试图下,右键,new Hadoop Location,新建一个hadoop位置,配置下面的Map/Reduce Master 和 DFS Master(即分别为jobtrackerhdfs的位置),注意要和hadoop安装时配置的一致。

6.配置完成后在左侧Project Explorer试图中就会显示一个DFS Locations, 这里可以图形化操纵hdfs文件系统,如新建文件夹,上传下载文件等。

7.新建一个map/reduce project在项目下新建三个类,分别为MaxTemperatureMapper.java, MaxTemperatureReducerMaxTemperature类。代码分别如下:

///1.Mapper--MaxTemperatureMapper 

package com.test.mapred;

 

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 MaxTemperatureMapper extends

Mapper<LongWritable, Text, Text, IntWritable> {

private static final int MISSING = 9999;

@Override

protected void map(LongWritable key, Text value,

Mapper<LongWritable, Text, Text, IntWritable>.Context context)

throws IOException, InterruptedException {

String line = value.toString();

String year = line.substring(15, 19);

int airTemperature;

if (line.charAt(87) == '+') {

airTemperature = Integer.parseInt(line.substring(88, 92));

} else {

airTemperature = Integer.parseInt(line.substring(87, 92));

}

String quality = line.substring(92, 93);

if (airTemperature != MISSING && quality.matches("[01459]")) {

context.write(new Text(year), new IntWritable(airTemperature));

}

}

 

}

 

///2.reducer--MaxTemperatureReducer 

package com.test.mapred;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Reducer;

 

public class MaxTemperatureReducer extends

Reducer<Text, IntWritable, Text, IntWritable> {

 

@Override

protected void reduce(Text key, Iterable<IntWritable> values,

Reducer<Text, IntWritable, Text, IntWritable>.Context context)

throws IOException, InterruptedException {

 

int maxValue = Integer.MIN_VALUE;

for (IntWritable value : values) {

maxValue = Math.max(maxValue, value.get());

}

context.write(key, new IntWritable(maxValue));

}

}

 

///3.主程序MaxTemperature 

package com.test.mapred;

import org.apache.hadoop.conf.Configured;

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.Tool;

import org.apache.hadoop.util.ToolRunner;

 

public class MaxTemperature extends Configured implements Tool {

 

@Override

public int run(String[] args) throws Exception {

if (args.length != 2) {

System.err

.println("Usage: MaxTemperature <input path> <output path>");

System.exit(-1);

}

 

Job job = new Job();

job.setJarByClass(MaxTemperature.class);

job.setJobName("max temperature");

 

FileInputFormat.addInputPath(job, new Path(args[0]));

FileOutputFormat.setOutputPath(job, new Path(args[1]));

 

job.setMapperClass(MaxTemperatureMapper.class);

job.setReducerClass(MaxTemperatureReducer.class);

 

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(IntWritable.class);

 

return job.waitForCompletion(true) ? 0 : 1;

}

 

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

int code = ToolRunner.run(new MaxTemperature(), args);

System.exit(code);

}

}

 

8.运行。

在项目上右键 —>Run AS > Run Configuration 在配置框中设置要运行的项目以及主类, 并且在Arguments栏目中配置主函数运行参数(即输入文件路径和输出文件路径),然后点击最下面的”Run”即可运行!

也可以将项目打包成jar文件,然后再在hadoop环境中使用hadoop jar  XXX.jar  MainClass  input_path  output_path这种方式运行!

至此,第一个MapReduce程序开发完成。

 

下面给出NCDC气象站的两年气象数据下载连接及密码。

链接:http://pan.baidu.com/s/1bnhIuo3 密码:pe5y

0 0
原创粉丝点击