spark分析航班总拖延时间

import csv
import matplotlib.pyplot as plt

from StringIO import StringIO
from datetime import datetime
from collections import namedtuple
from operator import add, itemgetter
from pyspark import SparkConf, SparkContext

APP_NAME = "Flight Delay Analysis"
DATE_FMT = "%Y-%m-%d"
TIME_FMT = "%H%M"

fields = ('date','airline','flightnum','origin','dest','dep',\
          'dep_delay','arv','arv_delay','airtime','distance')
Flight = namedtuple("Flight",fields)

##closure functions
def parse(row):
    '''
    Parse a row and returns a named tuple
    :param row:
    :return:
    '''
    row[0] = datetime.strptime(row[0], DATE_FMT).date()
    row[5] = datetime.strptime(row[5], TIME_FMT).time()
    row[6] = float(row[6])
    row[7] = datetime.strptime(row[7],TIME_FMT).time()
    row[8] = float(row[8])
    row[9] = float(row[9])
    row[10] = float(row[10])
    return Flight(*row[:11])

def split(line):
    '''
    Operator function for splitting a line with csv module
    :param line:
    :return:
    '''
    reader = csv.reader(StringIO(line))
    return reader.next()

def plot(delays):
    '''
    Show a bar chart of the total delay per airline
    :param depays:
    :return:
    '''
    airlines = [d[0] for d in delays]
    minutes = [d[1] for d in delays]
    index = list(xrange(len(airlines)))
    fig, axe = plt.subplots()
    bars = axe.barh(index, minutes)
    # Add the total minutes to the right
    for idx, air, min in zip(index, airlines, minutes):
        if min > 0:
            bars[idx].set_color('#d9230f')
            axe.annotate("%0.0f min" % min, xy=(min+1,idx+0.5),va = 'center')
        else:
            bars[idx].set_color("#469408")
            axe.annotate("%0.0f min" % min, xy=(10,idx+0.5),va='center')
    #Set the ticks
    ticks = plt.yticks([idx+0.5 for idx in index], airlines)
    xt = plt.xticks()[0]
    plt.xticks(xt, [' ']*len(xt))

    # Minimize chart junk
    plt.grid(axis='x',color='white',linestyle='-')
    plt.title('Total Minutes Delayed per Airline')
    plt.show()

def main(sc):
    '''
    Describe the trnasformations and actions used on the dataset, then
    plot the visualization on the output using matplotlib.
    :param sc:
    :return:
    '''
    ## Load the airlines lookup dictionary
    airlines = dict(sc.textFile("/test/airlines.csv").map(split).collect())

    ## Broadcast the lookup dictionary to cluster
    airline_lookup = sc.broadcast(airlines)

    ## Read the csv data into an RDD
    flights = sc.textFile("/test/flights.csv").map(split).map(parse)

    # Map the total delay to the airline (joined using the broadcast value)
    delays = flights.map(lambda f: (airline_lookup.value[f.airline],add(f.dep_delay, f.arv_delay)))

    # Reduce the total delay for the month to the airline
    delays = delays.reduceByKey(add).collect()
    delays = sorted(delays, key=itemgetter(1))

    ## Provide output from the driver
    for d in delays:
        print "%0.0f minutes delayed\t%s" % (d[1],d[0])

    ##Show a bar chart of the delays
    plot(delays)

if __name__ == "__main__":
    ##Configure Spark
    conf = SparkConf().setMaster("local[*]")
    conf = conf.setAppName(APP_NAME)
    sc = SparkContext(conf = conf)
    ## Execute the main functionality
    main(sc)

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于Spark航班大数据分析是指利用Spark框架对航班相关的大规模数据进行处理、分析和挖掘的过程。Spark是一个快速、通用的大数据处理引擎,它提供了分布式计算的能力,可以处理大规模数据集并支持复杂的数据处理任务。 在航班大数据分析中,Spark可以用于以下方面: 1. 数据清洗和预处理:航班数据通常包含大量的噪声和缺失值,需要进行清洗和预处理。Spark提供了丰富的数据处理函数和操作,可以对数据进行清洗、过滤、转换等操作,以便后续分析使用。 2. 数据聚合和统计:航班数据通常包含多个维度的信息,如航班号、起降时间、航空公司等。Spark可以利用其强大的聚合和统计功能,对航班数据进行分组、汇和统计分析,例如计算每个航空公司的平均延误时间、最繁忙的机场等。 3. 机器学习和预测分析:利用Spark的机器学习库(如MLlib)和图计算库(如GraphX),可以进行航班延误预测、航线推荐等任务。通过构建机器学习模型和图算法,可以挖掘航班数据中的潜在模式和关联规则。 4. 实时数据处理:航班数据通常是实时生成的,需要进行实时处理和分析Spark提供了流式处理框架(如Spark Streaming和Structured Streaming),可以对实时航班数据进行处理和分析,例如实时监控航班延误情况、实时预测航班准点率等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值