看了Hadoop权威指南第三版上面的计算最大温度的例子之后自己动手实践了一遍。下面是过程:
首先是数据,我只用了两个年份两个TXT文本:
接下来是MaxTemperatureMapper.java代码:
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
public void map(LongWritable key,Text value,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 != MISSI