作業報告
( 2019學年春季學期 )
課程名稱 |
大數據與深度學習 |
作業名稱 |
航班月延遲到達比例 |
組長 |
LFY |
學號 |
2016 |
組員 |
|
學號 |
|
組員 |
|
學號 |
|
組員 |
|
學號 |
|
組員 |
|
學號 |
|
專業 |
16 |
教師 |
|
1、問題描述
利用分佈式系統,利用map/reduce編寫程序統計每月航班延遲抵達的比例。
注意:航班數據集下載網址:http://stat-computing.org/dataexpo/2009/the-data.html
2、實現描述
按照老師提醒的思路,利用航班實際到達時間與擬抵達時間進行比較,若航班延遲,則在map階段寫出一個1加一個-1,否則只寫出一個1,到reduce階段統計出多少個1以及多少個-1就能實現該問題所求。
3、運行效果
輸入文本
輸入數據集爲書本提供的網站下載的2008年的數據集包,該表格只能截屏部分
輸出文本
運行日誌
4、源碼
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class FlightMonthDelayRatio {
// 分析航班的月的航班延誤比例map
public static class FlightDelayRatioMap extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private final static IntWritable NegativeOne = new IntWritable(-1);
private Text date = new Text();
@Override
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String[] fields = value.toString().split(",");
try {
int year = Integer.parseInt(fields[0]); // filter first raw
} catch (NumberFormatException e) {
return;
}
try {//以防非法數據行導致運行終止
String s=fields[0]+"年"+fields[1]+"月";
date.set(s); // date of month
}catch (NumberFormatException e) {
return;
}
if(fields[6].compareTo(fields[7])>0)
{
context.write(date, one);
context.write(date, NegativeOne);
}
else
context.write(date, one);
}
}
//每月航班延誤比例Reduce函數
public static class FlightDelayRatioReducer extends Reducer<Text,IntWritable,Text,Text> {
private Text result = new Text();
@Override
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
int negativeSum=0;
for (IntWritable val : values) {
if(val.get()>0)
sum += val.get();
else
negativeSum+=val.get();
}
result.set(" 航班延誤:"+String.valueOf(-negativeSum)+" 航班總數:"+String.valueOf(sum)+" 延誤比例:"+String.valueOf(((double)-negativeSum/sum)*100.0)+"%");
context.write(key, result);
}
}
//如果文件夾存在,則刪除文件夾
private static void removeOutputPath(Configuration conf, String output) throws IOException {
Path path = new Path(output);
FileSystem hdfs = path.getFileSystem(conf);
if (hdfs.exists(path))
hdfs.delete(path, true);
}
//每月航班延誤比例Job
private static Job createFlightDelayRatioJob(Configuration conf,String input, String output) throws IOException {
Job job = Job.getInstance(conf);
job.setJarByClass(FlightMonthDelayRatio.class);
job.setMapperClass(FlightDelayRatioMap.class);
//job.setCombinerClass(FlightDelayRatioReducer.class);
job.setReducerClass(FlightDelayRatioReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(input));
FileOutputFormat.setOutputPath(job, new Path(output));
return job;
}
//主函數
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: ScoreAnalysis <in> <out1>");
System.exit(2);
}
removeOutputPath(conf, otherArgs[1]);
Job job = createFlightDelayRatioJob(conf, otherArgs[0],otherArgs[1]);
job.waitForCompletion(true);
}
}
5、總結
通過此次作業,深刻體會到了,在大數據處理中,將map/reduce的單詞計數代碼稍加修改就能夠處理實際生活中的問題。