例子为获取气象台不同年份的最高气温,通过mapper和reduce处理后,得到最终的结果。
其中mapper为:
package mapper;
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.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;
/**
* @ version 创建时间:2014-4-2 上午09:28:47
*
* @ author leicl
*
* 类说明:主要是负责收集数据,对数据进行基本简单的整理
*
*/
@SuppressWarnings("deprecation")
public class MaxTempretrueMapper extends MapReduceBase implements
Mapper<LongWritable, Text, Text, IntWritable> {
@Override
public void map(LongWritable key, Text value,
OutputCollector<Text, IntWritable> output, Reporter report)
throws IOException {
// TODO Auto-generated method stub
String line = value.toString();
String year = line.substring(0,4);
int temp = Integer.parseInt(line.substring(6,9));
output.collect(new Text(year), new IntWritable(temp));
}
}
以下为reduce部分:
package reduce;
import java.io.IOException;
import java.util.Iterator;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
/**
* @ version 创建时间:2014-4-2 上午10:00:54
*
* @ author leicl
*
* 类说明:
1、 负责按照年份分类;
2、 计算分组后的年份中气温最高值
*
*/
public class MaxTempratureReduce extends MapReduceBase implements
Reducer<Text, IntWritable, Text, IntWritable> {
@Override
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
// TODO Auto-generated method stub
int maxValue = Integer.MIN_VALUE;
while(values.hasNext()){
maxValue = Math.max(maxValue, values.next().get());
}
output.collect(key, new IntWritable(maxValue));
}
}
以下为mapperreduce调用job任务,将mapper和reduce设置到job中进行运行:
package mapperReduce;
import java.io.IOException;
import mapper.MaxTempretrueMapper;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import reduce.MaxTempratureReduce;
/**
* @ version 创建时间:2014-4-2 上午09:41:36
*
* @ author leicl
*
* 类说明:
*
*/
public class MaxTemprature {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException {
JobConf conf = new JobConf(MaxTemprature.class);
conf.setJobName("max tempratrue");
FileInputFormat.addInputPath(conf, new Path("E:\\hadoop\\baseDate.txt"));//基础数据,需要处理的数据
FileOutputFormat.setOutputPath(conf, new Path("E:\\hadoop\\maxTem.txt"));//数据处理结果回写
conf.setMapperClass(MaxTempretrueMapper.class);//mapper类设置
conf.setReducerClass(MaxTempratureReduce.class);//reduce类设置
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
JobClient.runJob(conf);//启动jobtracker
}
}
其中源文件内容:(前面为年份,后面为气温,只是测试值)
1990-1111
2003-0121
1990-4051
2003-1311
1800-0451
回写结果:
1800 451
1990 51
2003 121