作业报告

                                                                             ( 2019学年春季学期 )

课程名称

大数据与深度学习

作业名称

航班月延迟到达比例

组长

LFY

学号

2016

组员

 

学号

 

组员

 

学号

 

组员

 

学号

 

组员

 

学号

 

专业

16

教师

 


1、问题描述

利用分布式系统,利用map/reduce编写程序统计每月航班延迟抵达的比例。

Map/Reduce实现求航班延迟比例

注意:航班数据集下载网址:http://stat-computing.org/dataexpo/2009/the-data.html 

2、实现描述

按照老师提醒的思路,利用航班实际到达时间与拟抵达时间进行比较,若航班延迟,则在map阶段写出一个1加一个-1,否则只写出一个1,到reduce阶段统计出多少个1以及多少个-1就能实现该问题所求。

3、运行效果

输入文本

输入数据集为书本提供的网站下载的2008年的数据集包,该表格只能截屏部分

Map/Reduce实现求航班延迟比例

输出文本

Map/Reduce实现求航班延迟比例

运行日志

Map/Reduce实现求航班延迟比例

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的单词计数代码稍加修改就能够处理实际生活中的问题。

相关文章: