mapreduce求ncdc气象数据中最低温度
程序员文章站
2022-03-05 11:38:17
...
网上对于ncdc求最低温度的mapreduce例子有多个版本,大多是hadoop1.x旧版本,且map函数使用的都是不匹配的输入数据格式,本文在hadoop2.7.3环境中运行ncdc气象 从数据求最低气温例子,从下载ncdc的气象数据到执行mapredeuce程序给出详细步骤:
1、下载数据:
下载地址:ftp://ftp.ncdc.noaa.gov/pub/data/ 有两个文件夹 /noaa 和 /gsod 文件格式分别为:
本例中使用gsod数据,只下载2016和2017数据:
通过wget下载:wget -r -c ftp://ftp.ncdc.noaa.gov/pub/data/gsod/2017
2、解压合并数据:
下载后的数据是多个gz包,需要处理成我们能用的文本数据。
$ zcat *.gz > ../ncdc.txt
查看12行数据:
$ head -12 ncdc.txt
数据格式说明:wget -r -c ftp://ftp.ncdc.noaa.gov/pub/data/gsod/readme.txt
删除标题行,使用sed命令删除匹配STN的行
$ sed -i '/STN/d' ncdc.txt
3、上传准备好的数据至hdfs:
hadoop fs -mkdir /in
hadoop fs -copyFromLocal ncdc.txt /in
4、编写求最低温度的mapreduce代码:
MinTemperature.java
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;
public class MinTemperature {
public static void main(String[] args) throws Exception {
if(args.length != 2) {
System.err.println("Usage: MinTemperature<input path> <output path>");
System.exit(-1);
}
Job job = new Job();
job.setJarByClass(MinTemperature.class);
job.setJobName("Min temperature");
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setMapperClass(MinTemperatureMapper.class);
job.setReducerClass(MinTemperatureReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
MinTemperatureMapper.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 MinTemperatureMapper 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(14, 18);
int airTemperature;
airTemperature =(int)Math.floor(Double.valueOf(line.substring(24, 30).trim()));
if(airTemperature != MISSING )
{
context.write(new Text(year), new IntWritable(airTemperature));
}
}
}
MinTemperatureReducer.java
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class MinTemperatureReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int minValue = Integer.MAX_VALUE;
for(IntWritable value : values) {
minValue = Math.min(minValue, value.get());
}
context.write(key, new IntWritable(minValue));
}
}
5、编译代码:
2.7.* 版本没有core这个包,需要在~/.bashrc这个文件中增加以下两列
$ gedit ~/.bashrc
export HADOOP_HOME=/soft/hadoop-2.7.2
export CLASSPATH=$($HADOOP_HOME/bin/hadoop classpath):$CLASSPATH
使其生效:
$ source ~/.bashrc
编译,这事没有错误
$javac *.java
打包,移动到上级目录,并删除所有class:
$jar cvf ./MinTemperature.jar ./*.class
mv *.jar ..
rm *.class
6、执行mapreduce最低气温例子
hadoop jar MinTemperature.jar MinTemperature /in/ncdc.txt /ncdc
可在此链接下载jar包:https://download.csdn.net/download/weixin_42026041/10446273
附上结果: