Using BETWEEN Operator¶
Let us understand the usage of BETWEEN
in conjunction with AND
while filtering data from Data Frames.
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 - Basic Transformations'). \
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
Tasks¶
Let us perform some tasks to understand filtering in detail. Solve all the problems by passing conditions using both SQL Style as well as API Style.
Read the data for the month of 2008 January.
airtraffic_path = "/public/airtraffic_all/airtraffic-part/flightmonth=200801"
airtraffic = spark. \
read. \
parquet(airtraffic_path)
airtraffic.printSchema()
root
|-- Year: integer (nullable = true)
|-- Month: integer (nullable = true)
|-- DayofMonth: integer (nullable = true)
|-- DayOfWeek: integer (nullable = true)
|-- DepTime: string (nullable = true)
|-- CRSDepTime: integer (nullable = true)
|-- ArrTime: string (nullable = true)
|-- CRSArrTime: integer (nullable = true)
|-- UniqueCarrier: string (nullable = true)
|-- FlightNum: integer (nullable = true)
|-- TailNum: string (nullable = true)
|-- ActualElapsedTime: string (nullable = true)
|-- CRSElapsedTime: integer (nullable = true)
|-- AirTime: string (nullable = true)
|-- ArrDelay: string (nullable = true)
|-- DepDelay: string (nullable = true)
|-- Origin: string (nullable = true)
|-- Dest: string (nullable = true)
|-- Distance: string (nullable = true)
|-- TaxiIn: string (nullable = true)
|-- TaxiOut: string (nullable = true)
|-- Cancelled: integer (nullable = true)
|-- CancellationCode: string (nullable = true)
|-- Diverted: integer (nullable = true)
|-- CarrierDelay: string (nullable = true)
|-- WeatherDelay: string (nullable = true)
|-- NASDelay: string (nullable = true)
|-- SecurityDelay: string (nullable = true)
|-- LateAircraftDelay: string (nullable = true)
|-- IsArrDelayed: string (nullable = true)
|-- IsDepDelayed: string (nullable = true)
airtraffic.count()
605659
Get count of flights departed late between 2008 January 1st to January 9th using FlightDate.
from pyspark.sql.functions import col, concat, lpad
airtraffic. \
withColumn("FlightDate",
concat(col("Year"),
lpad(col("Month"), 2, "0"),
lpad(col("DayOfMonth"), 2, "0")
)
). \
show()
+----+-----+----------+---------+-------+----------+-------+----------+-------------+---------+-------+-----------------+--------------+-------+--------+--------+------+----+--------+------+-------+---------+----------------+--------+------------+------------+--------+-------------+-----------------+------------+------------+----------+
|Year|Month|DayofMonth|DayOfWeek|DepTime|CRSDepTime|ArrTime|CRSArrTime|UniqueCarrier|FlightNum|TailNum|ActualElapsedTime|CRSElapsedTime|AirTime|ArrDelay|DepDelay|Origin|Dest|Distance|TaxiIn|TaxiOut|Cancelled|CancellationCode|Diverted|CarrierDelay|WeatherDelay|NASDelay|SecurityDelay|LateAircraftDelay|IsArrDelayed|IsDepDelayed|FlightDate|
+----+-----+----------+---------+-------+----------+-------+----------+-------------+---------+-------+-----------------+--------------+-------+--------+--------+------+----+--------+------+-------+---------+----------------+--------+------------+------------+--------+-------------+-----------------+------------+------------+----------+
|2008| 1| 16| 3| 1725| 1735| 1959| 2021| OH| 5367| N716CA| 154| 166| 146| -22| -10| BGR| CVG| 906| 1| 7| 0| null| 0| NA| NA| NA| NA| NA| NO| NO| 20080116|
|2008| 1| 17| 4| 1717| 1701| 1915| 1855| OH| 4977| N967CA| 118| 114| 101| 20| 16| SYR| CVG| 527| 2| 15| 0| null| 0| 16| 0| 4| 0| 0| YES| YES| 20080117|
|2008| 1| 17| 4| 1220| 1225| 1440| 1504| OH| 5352| N709CA| 140| 159| 117| -24| -5| SAV| BOS| 901| 8| 15| 0| null| 0| NA| NA| NA| NA| NA| NO| NO| 20080117|
|2008| 1| 17| 4| 1530| 1530| 1645| 1637| OH| 5426| N779CA| 75| 67| 45| 8| 0| CVG| GRR| 268| 5| 25| 0| null| 0| NA| NA| NA| NA| NA| YES| NO| 20080117|
|2008| 1| 17| 4| 1203| 1205| 1429| 1429| OH| 5441| N809CA| 86| 84| 58| 0| -2| STL| CVG| 307| 3| 25| 0| null| 0| NA| NA| NA| NA| NA| NO| NO| 20080117|
|2008| 1| 18| 5| 1150| 1150| 1457| 1524| OH| 5220| N436CA| 127| 154| 102| -27| 0| STL| JFK| 892| 4| 21| 0| null| 0| NA| NA| NA| NA| NA| NO| NO| 20080118|
|2008| 1| 18| 5| 1215| 1009| 1540| 1251| OH| 5260| N446CA| 145| 102| 140| 169| 126| MCI| CVG| 539| 2| 3| 0| null| 0| 126| 0| 43| 0| 0| YES| YES| 20080118|
|2008| 1| 19| 6| 835| 835| 1145| 1130| OH| 5276| N523CA| 130| 115| 83| 15| 0| TUL| CVG| 646| 4| 43| 0| null| 0| 0| 0| 15| 0| 0| YES| NO| 20080119|
|2008| 1| 20| 7| 1925| 1935| 2148| 2124| OH| 5215| N729CA| 143| 109| 34| 24| -10| JFK| PHL| 94| 5| 104| 0| null| 0| 0| 0| 24| 0| 0| YES| NO| 20080120|
|2008| 1| 20| 7| 825| 830| 1045| 1007| OH| 5324| N933CA| 140| 97| 92| 38| -5| RDU| CVG| 390| 1| 47| 0| null| 0| 0| 0| 38| 0| 0| YES| NO| 20080120|
|2008| 1| 21| 1| 1638| 1640| 1741| 1754| OH| 4984| N719CA| 63| 74| 45| -13| -2| CVG| DTW| 229| 10| 8| 0| null| 0| NA| NA| NA| NA| NA| NO| NO| 20080121|
|2008| 1| 21| 1| 1200| 1204| 1600| 1559| OH| 5056| N371CA| 180| 175| 147| 1| -4| MSY| LGA| 1183| 13| 20| 0| null| 0| NA| NA| NA| NA| NA| YES| NO| 20080121|
|2008| 1| 21| 1| 1950| 1935| 2103| 2124| OH| 5215| N965CA| 73| 109| 29| -21| 15| JFK| PHL| 94| 8| 36| 0| null| 0| NA| NA| NA| NA| NA| NO| YES| 20080121|
|2008| 1| 21| 1| 1950| 1830| 2225| 2007| OH| 5595| N641CA| 155| 97| 136| 138| 80| DCA| JFK| 213| 10| 9| 0| null| 0| 0| 80| 58| 0| 0| YES| YES| 20080121|
|2008| 1| 21| 1| 700| 700| 955| 950| OH| 5610| N964CA| 115| 110| 87| 5| 0| HSV| DCA| 613| 8| 20| 0| null| 0| NA| NA| NA| NA| NA| YES| NO| 20080121|
|2008| 1| 22| 2| 2020| 1910| 2223| 2125| OH| 5032| N538CA| 63| 75| 44| 58| 70| ORD| CVG| 264| 1| 18| 0| null| 0| 0| 58| 0| 0| 0| YES| YES| 20080122|
|2008| 1| 22| 2| 1320| 1320| 1600| 1528| OH| 5331| N805CA| 160| 128| 102| 32| 0| CVG| JFK| 589| 12| 46| 0| null| 0| 0| 0| 32| 0| 0| YES| NO| 20080122|
|2008| 1| 23| 3| 908| 908| 1216| 1149| OH| 5033| N963CA| 188| 161| 124| 27| 0| LGA| SAV| 722| 6| 58| 0| null| 0| 0| 0| 27| 0| 0| YES| NO| 20080123|
|2008| 1| 23| 3| 1245| 1252| 1409| 1430| OH| 5050| N427CA| 84| 98| 73| -21| -7| CLT| CVG| 335| 1| 10| 0| null| 0| NA| NA| NA| NA| NA| NO| NO| 20080123|
|2008| 1| 23| 3| 630| 635| 840| 831| OH| 5355| N926CA| 130| 116| 85| 9| -5| GSP| LGA| 610| 20| 25| 0| null| 0| NA| NA| NA| NA| NA| YES| NO| 20080123|
+----+-----+----------+---------+-------+----------+-------+----------+-------------+---------+-------+-----------------+--------------+-------+--------+--------+------+----+--------+------+-------+---------+----------------+--------+------------+------------+--------+-------------+-----------------+------------+------------+----------+
only showing top 20 rows
airtraffic. \
withColumn("FlightDate",
concat(col("Year"),
lpad(col("Month"), 2, "0"),
lpad(col("DayOfMonth"), 2, "0")
)
). \
filter("""
IsDepDelayed = 'YES' AND
Cancelled = 0 AND
FlightDate BETWEEN 20080101 AND 20080109
"""). \
count()
86180
airtraffic. \
withColumn("FlightDate",
concat(col("Year"),
lpad(col("Month"), 2, "0"),
lpad(col("DayOfMonth"), 2, "0")
)
). \
filter("""
IsDepDelayed = 'YES' AND
Cancelled = 0 AND
FlightDate >= 20080101 AND
FlightDate <= 20080109
"""). \
count()
86180
API Style
from pyspark.sql.functions import col
c = col('x')
help(c.between)
Help on method between in module pyspark.sql.column:
between(lowerBound, upperBound) method of pyspark.sql.column.Column instance
A boolean expression that is evaluated to true if the value of this
expression is between the given columns.
>>> df.select(df.name, df.age.between(2, 4)).show()
+-----+---------------------------+
| name|((age >= 2) AND (age <= 4))|
+-----+---------------------------+
|Alice| true|
| Bob| false|
+-----+---------------------------+
.. versionadded:: 1.3
airtraffic. \
withColumn("FlightDate",
concat(col("Year"),
lpad(col("Month"), 2, "0"),
lpad(col("DayOfMonth"), 2, "0")
)
). \
filter((col("IsDepDelayed") == "YES") &
(col("Cancelled") == 0) &
(col("FlightDate").between(20080101, 20080109))
). \
count()
86180
Get count of flights arrived late between 15 minutes and 60 minutes.
airtraffic. \
filter("""
ArrDelay BETWEEN 15 AND 60
"""). \
count()
105319
API Style
airtraffic. \
filter((col("ArrDelay").between(15, 60))
). \
count()
105319