# Standard library: dates, timing and paths
import time
from datetime import datetime
from functools import wraps
from pathlib import Path
# Data and display
import pandas as pd
from IPython.display import Image
# Spark
from pyspark import Row
from pyspark.sql import SparkSession
from pyspark.sql.types import *
from pyspark.sql.functions import *<span style="background: #F2A65A; color: #14213D; font-weight: 700; font-size: 13px; padding: 4px 10px; border-radius: 12px;">Module 6 · Spark DataFrames and Pipelines</span>
<span style="background: #D7E6E4; color: #14213D; font-weight: 600; font-size: 13px; padding: 4px 10px; border-radius: 12px; margin-left: 6px;">⏱ about TODO minutes</span>
Working with Spark DataFrames
| Objective | Go deeper into the Spark DataFrame API: create, change and query DataFrames and apply user-defined functions. |
| You will | Create DataFrames with and without a schema · Select, add and rename columns, filter and group rows · Write and apply user-defined functions (UDFs) |
| Data | data/raw/activity_log_raw.csv (8.3 GB, USB drive) · data/raw/hh_data.txt (TODO size and source) · data/raw/city.list.json (TODO size, OpenWeather bulk download) |
| Core packages | pyspark · pandas |
When it comes to all the strucutured APIs in Spark, the DataFrames API is probably the most commonly used way to interact with Spark functionality because its most familiar with most users. In this notebook, we will dive deeper into the DataFrames API and explore the following topics:
- Creating Spark DataFrames with Schema and the advantage of using schemas
- Manipulating DataFrames (e.g., adding colummns, renaming columns, changing data types etc
- Query DataFrames. How do you filter data?
- Performing common data analysis tasks on DataFrames (e.g., grouping data, applying functions)
- Applying user defined functions in Spark DataaFrames
- Switching between DataFrames and other data structures (e.g., Pandas, RDD’s etc)
Requirements: pyspark and Java 17 (see Setup), pandas.
Data: activity_log_raw.csv, hh_data.txt and city.list.json in data/raw/.
0. Setup and Configuration
Working Directory and Input Files
Lets provide paths to input files we will use. Its a good practice to create these as global variables. Also, use Python module Path from pathlib to manage file paths.
# ============================================================
# BASE WORKING DIRECTORIES
# ============================================================
# The course folder is the closest folder, going upwards, that contains environment.yml
DIR_COURSE = next(
(p for p in [Path.cwd(), *Path.cwd().parents] if (p / "environment.yml").exists()), None
)
if DIR_COURSE is None:
DIR_COURSE = Path.cwd()
print("Running outside the course folder (e.g. Colab): upload the datasets to data/raw/.")
DIR_DATA = DIR_COURSE / "data" / "raw"
# ============================================================
# INPUT FILES
# ============================================================
# Activity_log_raw
FILE_ACTIVITY_LOG = DIR_DATA / "activity_log_raw.csv"
# path to hh_data.txt
FILE_HH_DATA = DIR_DATA / "hh_data.txt"
# OpenWeather city list
FILE_CITY_LIST = DIR_DATA / "city.list.json"
# Overview of Spark SQL functions (same location as the old "../DOCS/images/" path;
# TODO: this image is not in the repository yet)
DIR_IMAGES = DIR_COURSE / "notebooks" / "DOCS" / "images"
FILE_IMG_SPARK_FUNCTIONS = DIR_IMAGES / "spark-functions.png"# Altenatively, you can put a full-path to wheree your data is located like below
# DATA_DIR = Path(full-path-folder-where-you-are-keeping-data)
DATA_DIR = DIR_COURSE / "data"
# Activity_log_raw
ACTIV_LOG = FILE_ACTIVITY_LOG
# path to hh_data.txt
HH_DATA = FILE_HH_DATAGlobal Variables for Processing
# ============================================================
# SPARK SETTINGS
# ============================================================
SPARK_MASTER = "local[*]"
SPARK_APP_NAME = "DataFrameFromPythonObj"
# ============================================================
# COLUMN NAMES
# ============================================================
COL_ACTIVITY_TIME = "ACTIVITY_TIME"
COL_STATUS = "STATUS"1. Composition of a Spark DataFrame
In Spark, a DataFrame object consists of Row objects and Column objects. Concretely, each row of a Spark DataFrame is an instance of the pyspark.sql.Row while each column is an instance of the pyspark.sql.Column class. We will look at each of these classes in detail.
2. Creating DataFrames
There are three main ways to create a Spark DataFrame as follows:
- From Python objects
- External data sources
- Other Spark objects
Schemas
Also, when creating DataFrames, you have the option to use a schema or not. A schema in Spark defines the column names and associated data types for a DataFrame. Most often, schemas come into play when you are reading structured data from an external data source. When a schema is not used, Spark has to infer the data type which can slow your application if you have a massive dataset. Although schemas are more of DBMS language but they offer several advantages when dealing with large datasets:
- Spark doesnt have to infer data types, so you get speed benefits.
- Without a schema, Spark creates a separate job just to read a large portion of your file to ascertain the schema, which for a large data file can be expensive and time-consuming. As such, defining a schema will avoid this.
- You can detect errors early if data doesn’t match the schema. #### Defining Schemas
- Programmatically using Spark DataTypes
- Using Data Definition Language (DDLs)
Spark DataFrame from Python objects
Given a list of data (in most cases its going to be a nested list), how do we convert it into a Spark DataFrame. There are several depending on the input Pytho objects. Here are some:
- Define a schema for the new dataframe. The schema helps Spark understand the column data types
- Convert the regular Python objects into
Rowswhich we can the use to create a DataFrame
# initialize SparkSession
spark = SparkSession.builder.master(SPARK_MASTER).appName(SPARK_APP_NAME).getOrCreate()# The data
data = [[1, "Jules", "Damji", "https://tinyurl.1", "1/4/2016", 4535, ["twitter","LinkedIn"]],
[2, "Brooke","Wenig", "https://tinyurl.2", "5/5/2018", 8908, ["twitter",
"LinkedIn"]],
[3, "Denny", "Lee", "https://tinyurl.3", "6/7/2019", 7659, ["web",
"twitter", "FB", "LinkedIn"]],
[4, "Tathagata", "Das", "https://tinyurl.4", "5/12/2018", 10568,
["twitter", "FB"]],
[5, "Matei","Zaharia", "https://tinyurl.5", "5/14/2014", 40578, ["web",
"twitter", "FB", "LinkedIn"]],
[6, "Reynold", "Xin", "https://tinyurl.6", "3/2/2015", 25568,
["twitter", "LinkedIn"]]
]
# examine the data
data[0]# Define schema for our data using DDL, in most cases whe
schema_twitter = "num INT, first_name STRING, last_name STRING, url STRING, date STRING, some_num INT, tweet STRING"# Create a DataFrame using the schema defined above
sdf_python_obj_schema = spark.createDataFrame(data, schema=schema_twitter)
print(sdf_python_obj_schema.printSchema())# Convert list of items into Spark Rows
spark_rows = []
for item in data:
row = Row(num=item[0], first_name=item[1], last_name=item[2],
url=item[3], date=item[4], some_num=item[5], tweet=item[6])
spark_rows.append(row)sdf_from_rows = spark.createDataFrame(spark_rows)sdf_from_rows.show()Rows
A row in Spark is a generic Row object, containing one or more columns. Each column may be of the same data type (e.g., integer or string), or they can have different types (integer, string, map, array, etc.). Because Row is an object in Spark and an ordered collection of fields, you can instantiate a Row the same way we instantiate any object. Consequently, you can collect Row objects in a list and create a Spark DataFrame.
# import row class
first_five_rows = sdf.head(5)
type(first_five_rows[0])
# create row
row = Row(name="Alice", age=11)
# A list of rows
rows = [Row(name="Matei Zaharia", state="CA"), Row(name="Reynold Xin", state="CA")]
# Create a DataFrame from Row objects
spark_df_from_rows = spark.createDataFrame(rows)
spark_df_from_rows.show()EXERCISE-1: Creating a Spark DataFrame with Rows. Please complete the function below and call it.
def convert_json_to_spark_with_rows(json_file):
# Load json into pandas DataFrame
# create a list to hold all Row objects
rows = YOUR CODE
for idx, row in df.iterrows():
# get lon and lat from the coord column using indexing, dict key access
x = row['coord']['lon']
y = row['coord']['lat']
# create the Row object here
srow = YOUR CODE
# append this row object to the list
YOUR CODE
# When creating Spark DataFrame this way, its better to use schema to avoid troubles
# create a schema for this data here, use DOUBLE as data type for lon and lat
schema = YOUR CODE
# use spark.createDataFrame() here
# if yiu get errors, use the option verifySchema=False
spark_df = YOUR CODE
# use show() statement to show the DataFrame
# use show() with print to ensure we see the outputs
YOUR CODEjsonfile = str(DATA_DIR.joinpath("raw", "city.list.json")
#convert_json_to_spark_with_rows(jsonfile)
df.show()EXERCISE-2: READ CSV WITH SCHEMA
- Use Spark documentation on how to read from file with a define schema. Note, the schema is what we arleady defined above. The data above has been saved as
blog_simple_dataset.csv. Read it as a Spark DataFrame with schema. Answer this question in the next cell. - Define schema for the
activity_raw_data.csvuse string for the datetime column - Load the dataset with and without schema using the functions defined below. Compare the loading times. Answer this question by completing the functions defined below and calling them.
def timefn(fn):
"""
Function for recording running time of a function
"""
@wraps(fn)
def measure_time(*args, **kwargs):
t1 = time.time()
result = fn(*args, **kwargs)
t2 = time.time()
print("@timefn:" + fn.__name__ + " took " + str(t2 - t1) + " seconds")
return result
return measure_time@timefn
def load_with_schema(large_csv):
# define the schema here using DDL
# you can load part of the file with pandas (just a few rows)
# to remind yourself of the data types
schema = "`SID` INT, `ACTIVITY_ID` INT, `Last` STRING, `ACTIVITY_TIME` STRING,`STATUS` STRING"
spark = SparkSession.builder.master("local[*]").appName("ReadWithChema").getOrCreate()
# Now read the data
sdf = spark.read.schema(schema).csv(large_csv)
print(sdf.show())@timefn
def load_without_schema(large_csv):
spark = SparkSession.builder.master("local[*]").appName("DataFrameFromPythonObj").getOrCreate()
sdf = spark.read.csv(large_csv, header=True)
print(sdf.show())load_with_schema(str(ACTIV_LOG))load_without_schema(str(ACTIV_LOG))The difference shown above will of course become signficant and consequential when the number of columns and size of data increase. As such =, although not required, in some use cases, you can use schema to improve performance.
Spark DataFrame from external data sources
The most common way (which we have already seen) is to load data from exteernal data sources and Spark supports numerous data stores. Spark reads data through the DataFrameReaderobject. Please look at the documeentation here to see all data sources that the Spark DataFrameReaderobject supports.
3. Manipulating Spark DataFrames
Columns and Expressions in DataFrames
In Spark DataFrames, columns behave like pandas DataFrames in several ways but they also behave different. You can list all the columns by their names, and you can perform operations on their values using relational or computational expressions.
- Column is the name of the object, which has many import methods such as describe while col() is a standard built-in function that returns a Column.
We need to use the col() and expr() function available in pyspark,sql.functions() for many operations such as:
- Add, rename columns
- Subset data based on columns
- Access columns to compute stats on them
- Access columns to compute operations on them such as sorting
# Lets use Activity_log_raw.csv for trying out manipulations
sdf = spark.read.csv(str(ACTIV_LOG), header=True)Selecting columns and rows
How to select a single column and/or a subset of rows. We use select() to pick one or multipe. However, some of the same indexing we use in Pandas work,
# Find unnique values in column 'P21'
# Note that you have to use colleect() or show()
# to bring results to driver and see, other this is a transformation
sdf2 = sdf.select(COL_ACTIVITY_TIME, COL_STATUS)
sdf2.show()# Use of the Pandas like indexing seem to also be supported like this
# but just experiment with it because not everything is supported
sdf_two_cols = sdf[[COL_ACTIVITY_TIME, COL_STATUS]]
sdf_two_cols.show(5)Add a new column using expr and col
In order to add a new column in a Spark DataFrame, we use the DataFrame.withColumn(new_col_name, expression_to_compute_new_col). For that, we need to import the col and expr functions from the pyspark.sql.functions module.
sdf.show(5)# use expr
sdf2 = sdf.withColumn("status_S", (expr('STATUS = "S"')))
sdf2.show()# use the col function which I prefer over the expr col("Hits")
sdf2 = sdf.withColumn("new_col", col(COL_STATUS) == 'S')
sdf2.show(10)EXERCISE-3:
Check if these statements: df.select(expr(“ACTIVITY_TIME”)).show(2), df.select(col(“ACTIVITY_TIME”)).show(2) and df.select(“ACTIVITY_TIME”).show(2) will provide the same output. Replace df with name of your Spark DataFrame.
Create a new DataFrame using expr to get only those rows where STATUS is “S” Note that expr() just perfoms the operation, it doesnt filter our the rows which evaluate to false.
Sort DataFrame: use the col function to sort the DataFrame on “SID” column
EXERCISE-4: USING THE HH_DATA, TRY OUT THESE COMMON OPERATIONS ON A DATAFRAME
- Using the
filter()function create a DataFrame of people who are 5 years and younger? - Find household size using
groupby()on the hh_id column and appropriate function (e.g., count, avg) - Add the household size to the
sdf_hhdataframe - Rename columns as follows:
P08-->age,P07M-->dob_monthandP07A-->dob_yr - Find how many null values are there in the age column and drop all nulls in the DataFrame
# Load the dataframe
sdf_hh = spark.read.csv(str(HH_DATA),header=True, sep="\t")sdf_hh.show(5)# Question-1
# Get dataframe for undr 5 years
sdf_hh2 = sdf_hh.withColumnRenamed('P08', 'age')
sdf_hh_u5 = sdf_hh2.filter('age <= 5')
total_pop = sdf_hh.count()
cnt_u5_children = sdf_hh_u5.count()
print('There are {:,} under-five children in the country out of a total of {:,} people'.format(
cnt_u5_children, total_pop))There are 4,448,568 under-five children in the country out of a total of 25,674,196 people
# Question-2
# Create a new dataframe called sdf_hh_size which
# gives number of persons per household by grouping by
# hh_id
sdf_hh_size = sdf_hh2.groupby('hh_id').count()
sdf_hh_size.show(5)# Add the hh_size column to the sdf_hh dataframe
# using joinsdf_hh3 = sdf_hh2.join(sdf_hh_size, on='hh_id')
sdf_hh3.cache()
sdf_hh3.show(5)# change column name from 'count' to hh_size
sdf_hh4 = sdf_hh3.withColumnRenamed('count', 'hh_size')
sdf_hh4.show(5)# Question-: Rename columns as follows:
# P08-->age, P07M-->dob_month and P07A-->dob_yr
sdf_hh5 = (sdf_hh4
.withColumnRenamed('P07M', 'dob_month')
.withColumnRenamed('P07A', 'dob_yr'))
sdf_hh5.show(5)4. Spark-SQL functions
As usual refer to the Pyspark API documentation for an overview of spark modules and core classes. So far, we have been interacting with spark-SQL. Within the spark-SQL, we have worked with the following:
- DataFrame API
- Row
- Spark Session API
- Functions
For functions, we have only used simple functions such as count and avg used in aggregation. Now, lets look at user defined functions (udf). Although new and more functions are continuously being added to the spark APIs, you will often find yourself in a situation where you need to define a small Python function to apply on spark DataFrame or other data types in spark.
Image(str(FILE_IMG_SPARK_FUNCTIONS), width=700)Creating and using user defined functions
Spark provides two types of user defined functions (UDF):
- Simple UDF. We use the
udfkeyword to define a UDF using lambda. When using thedefkeyword, we useudfas a decorator. When defining UDF, specifying the Spark Data type to be returned is important though in some cases (e.g., for strings) you can getaway without specifying the data type. Once defined, you can call the UDF by usingcol()to select a single column or multiple columns as input into the UDF. - Pandas UDF. With pandas on Spark API now availablee, not sure this function is useful anymore but it probably is. Esseentially, you use pandas functitons on a small slice of a Spark DataFrame.
How can we use user defined functions in this dataframe?
For this sdf dataframe, I want to know the earliest activity date. For that, I need to convert ACTIVITY_TIME from String to Spark DateType or TimestampType
sdf_act = spark.read.csv(str(ACTIV_LOG), header=True)
sdf_act.show(5)+---+-----------+--------------------+------+
|SID|ACTIVITY_ID| ACTIVITY_TIME|STATUS|
+---+-----------+--------------------+------+
|584| 1291|13-APR-15 10.33.4...| S|
|584| 1286|13-APR-15 10.33.4...| S|
|584| 1285|13-APR-15 10.33.4...| S|
|584| 1284|13-APR-15 10.33.4...| S|
|584| 1288|13-APR-15 10.33.4...| S|
+---+-----------+--------------------+------+
only showing top 5 rows
# Use Spark UDF to add date and datetime with lambda setup
# Note that in this case, the return data typee is Spark TimestampType(), DateType()
# date_fmt1='%Y%m%d%H%M%S'
# For dateitme, you need to provide the datetime format of the string
# please see here: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
# for more details on converting from datetime string
# to datetime Python object
date_fmt2 = "%d-%b-%y %H.%M.%S"
# define UDF
add_datetime = udf(lambda x: datetime.strptime(x[:-13], date_fmt2), TimestampType())
add_date = udf(lambda x: datetime.strptime(x[:-13], date_fmt2), DateType())# Add datetime and date column with our functions above
sdf_act2 = sdf_act.withColumn('datetime', add_datetime(col(COL_ACTIVITY_TIME)))
sdf_act3 = sdf_act2.withColumn('date', add_date(col(COL_ACTIVITY_TIME)))
sdf_act3.show(5)+---+-----------+--------------------+------+-------------------+----------+
|SID|ACTIVITY_ID| ACTIVITY_TIME|STATUS| datetime| date|
+---+-----------+--------------------+------+-------------------+----------+
|584| 1291|13-APR-15 10.33.4...| S|2015-04-13 10:33:42|2015-04-13|
|584| 1286|13-APR-15 10.33.4...| S|2015-04-13 10:33:42|2015-04-13|
|584| 1285|13-APR-15 10.33.4...| S|2015-04-13 10:33:42|2015-04-13|
|584| 1284|13-APR-15 10.33.4...| S|2015-04-13 10:33:42|2015-04-13|
|584| 1288|13-APR-15 10.33.4...| S|2015-04-13 10:33:42|2015-04-13|
+---+-----------+--------------------+------+-------------------+----------+
only showing top 5 rows
# Sort by date find the earliest and oldest
sdf_act3_sorted = sdf_act3.sort(col('date'))
earliest = sdf_act3_sorted.head()org.apache.spark.api.python.PythonException: Traceback (most recent call last):
File "/var/folders/w3/dfvgjkh10wz8t573m_c20xf00000gp/T/ipykernel_73734/873190141.py", line 12, in <lambda>
TypeError: 'NoneType' object is not subscriptable
460)
File "/var/folders/w3/dfvgjkh10wz8t573m_c20xf00000gp/T/ipykernel_73734/873190141.py", line 12, in <lambda>
TypeError: 'NoneType' object is not subscriptable
sdf_act3.sort?# Exercise: Try outt UDF on the activity_log_raw file
sdf = spark.read.csv(CSV_PATH, header=True)EXERCISE-5: AGE FROM DOB
Use the hh_data.txt to do the following:
- create a column called
age_from_dobwhich calculates age from the dob_yr column. - Drop all observations where
age_from_dobandagedont match
Summary
- TODO
- TODO
- TODO
Next: ML with large datasets