Solutions - Problem 6ΒΆ

Get the total number of flights per airport that do not contain entries in airport-codes.

  • This is an example for outer join.

  • We need to get number of flights per airport from the 2008 January airtraffic data which do not have entries in airport-codes.

  • Based on the side of the airtraffic data set, we can say left or right. We will be invoking join using airtraffic and hence we will use left outer join..

  • We will be peforming join first and then we will aggregate to get number of flights from the concerned airports per airport.

  • In this case will get total number of flights per airport.

Let us start spark context for this Notebook so that we can execute the code provided. You can sign up for our 10 node state of the art cluster/labs to learn Spark SQL using our unique integrated LMS.

from pyspark.sql import SparkSession

import getpass
username = getpass.getuser()

spark = SparkSession. \
    builder. \
    config('spark.ui.port', '0'). \
    config("spark.sql.warehouse.dir", f"/user/{username}/warehouse"). \
    enableHiveSupport(). \
    appName(f'{username} | Python - Joining Data Sets'). \
    master('yarn'). \
    getOrCreate()

If you are going to use CLIs, you can use Spark SQL using one of the 3 approaches.

Using Spark SQL

spark2-sql \
    --master yarn \
    --conf spark.ui.port=0 \
    --conf spark.sql.warehouse.dir=/user/${USER}/warehouse

Using Scala

spark2-shell \
    --master yarn \
    --conf spark.ui.port=0 \
    --conf spark.sql.warehouse.dir=/user/${USER}/warehouse

Using Pyspark

pyspark2 \
    --master yarn \
    --conf spark.ui.port=0 \
    --conf spark.sql.warehouse.dir=/user/${USER}/warehouse
spark.conf.set("spark.sql.shuffle.partitions", "2")
airtrafficPath = "/public/airtraffic_all/airtraffic-part/flightmonth=200801"
airtraffic = spark. \
    read. \
    parquet(airtrafficPath)
airtraffic. \
    select(
        "Year", "Month", "DayOfMonth", 
        "Origin", "Dest", "CRSDepTime"
    ). \
    show()
+----+-----+----------+------+----+----------+
|Year|Month|DayOfMonth|Origin|Dest|CRSDepTime|
+----+-----+----------+------+----+----------+
|2008|    1|        16|   BGR| CVG|      1735|
|2008|    1|        17|   SYR| CVG|      1701|
|2008|    1|        17|   SAV| BOS|      1225|
|2008|    1|        17|   CVG| GRR|      1530|
|2008|    1|        17|   STL| CVG|      1205|
|2008|    1|        18|   STL| JFK|      1150|
|2008|    1|        18|   MCI| CVG|      1009|
|2008|    1|        19|   TUL| CVG|       835|
|2008|    1|        20|   JFK| PHL|      1935|
|2008|    1|        20|   RDU| CVG|       830|
|2008|    1|        21|   CVG| DTW|      1640|
|2008|    1|        21|   MSY| LGA|      1204|
|2008|    1|        21|   JFK| PHL|      1935|
|2008|    1|        21|   DCA| JFK|      1830|
|2008|    1|        21|   HSV| DCA|       700|
|2008|    1|        22|   ORD| CVG|      1910|
|2008|    1|        22|   CVG| JFK|      1320|
|2008|    1|        23|   LGA| SAV|       908|
|2008|    1|        23|   CLT| CVG|      1252|
|2008|    1|        23|   GSP| LGA|       635|
+----+-----+----------+------+----+----------+
only showing top 20 rows
airtraffic.count()
605659
airportCodesPath = "/public/airtraffic_all/airport-codes"
def getValidAirportCodes(airportCodesPath):
    airportCodes = spark. \
        read. \
        option("sep", "\t"). \
        option("header", True). \
        option("inferSchema", True). \
        csv(airportCodesPath). \
        filter("!(State = 'Hawaii' AND IATA = 'Big')")
    return airportCodes
airportCodes = getValidAirportCodes(airportCodesPath)
airportCodes.count()
524
airtraffic. \
    join(airportCodes, airtraffic["Origin"] == airportCodes["IATA"], "left"). \
    filter("IATA IS NULL"). \
    select(airtraffic["Year"], airtraffic["Month"], airtraffic["DayOfMonth"], 
           airtraffic["Origin"], airtraffic["Dest"], airtraffic["CRSDepTime"], 
           airportCodes["*"]
          ). \
    show()
+----+-----+----------+------+----+----------+----+-----+-------+----+
|Year|Month|DayOfMonth|Origin|Dest|CRSDepTime|City|State|Country|IATA|
+----+-----+----------+------+----+----------+----+-----+-------+----+
|2008|    1|         8|   HDN| DEN|      1403|null| null|   null|null|
|2008|    1|        26|   HDN| DEN|      1533|null| null|   null|null|
|2008|    1|        26|   SJU| CLT|      1520|null| null|   null|null|
|2008|    1|        19|   SJU| ATL|       945|null| null|   null|null|
|2008|    1|        31|   ITO| HNL|      1010|null| null|   null|null|
|2008|    1|        26|   ITO| HNL|      1725|null| null|   null|null|
|2008|    1|        24|   KOA| LAX|      2355|null| null|   null|null|
|2008|    1|        22|   SJU| EWR|      1845|null| null|   null|null|
|2008|    1|        21|   SJU| MIA|      1815|null| null|   null|null|
|2008|    1|        26|   STT| MIA|       845|null| null|   null|null|
|2008|    1|        11|   SJU| MIA|      1235|null| null|   null|null|
|2008|    1|        31|   SJU| DFW|      1720|null| null|   null|null|
|2008|    1|         5|   SJU| MIA|      2110|null| null|   null|null|
|2008|    1|        30|   ITO| HNL|       915|null| null|   null|null|
|2008|    1|         1|   KOA| HNL|      1245|null| null|   null|null|
|2008|    1|         1|   KOA| HNL|      1030|null| null|   null|null|
|2008|    1|        28|   ITO| HNL|      1153|null| null|   null|null|
|2008|    1|         3|   OTZ| ANC|      1502|null| null|   null|null|
|2008|    1|         8|   OTZ| OME|       829|null| null|   null|null|
|2008|    1|        20|   SJU| JFK|      1245|null| null|   null|null|
+----+-----+----------+------+----+----------+----+-----+-------+----+
only showing top 20 rows
from pyspark.sql.functions import lit, col, count
airtraffic. \
    join(airportCodes, airtraffic["Origin"] == airportCodes["IATA"], "left"). \
    filter("IATA IS NULL"). \
    groupBy("Origin"). \
    agg(count(lit(1)).alias("FlightCount")). \
    orderBy(col("FlightCount").desc()). \
    show()
+------+-----------+
|Origin|FlightCount|
+------+-----------+
|   SJU|       1997|
|   KOA|       1316|
|   ITO|        786|
|   HDN|        429|
|   STT|        311|
|   BQN|        124|
|   PSE|        110|
|   OTZ|         92|
|   CEC|         88|
|   PSG|         62|
|   SCC|         62|
|   PMD|         57|
|   SLE|         54|
|   CDC|         48|
|   STX|         40|
|   ADK|          9|
+------+-----------+