# Standard library: timing and paths
import time
from functools import wraps
from pathlib import Path
# Data
import pandas as pd
# Spark
from pyspark.sql import SparkSession
from pyspark.sql.functions import count<span style="background: #F2A65A; color: #14213D; font-weight: 700; font-size: 13px; padding: 4px 10px; border-radius: 12px;">Module 5 · Apache Spark Fundamentals</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>
Hello Spark
| Objective | Run your first Spark application in Python and compare Spark with pandas on a large CSV file. |
| You will | Build a SparkSession and read CSV files into Spark DataFrames · Compare running times for Spark and pandas · Explore partitions, transformations, actions and the Spark UI |
| Data | data/raw/synthetic-cdrs/ (~12.5 GB, USB drive) · data/raw/synthetic-cell-locs.csv (35 KB, USB drive) · data/raw/activity_log_raw.csv (8.3 GB, USB drive) |
| Core packages | pyspark · pandas |
In this tutorial, you will run our first Spark application in Python. Thus, this our Spark Driver process. We will cover the following items:
- Build a SparkSession
- Use the high level API DataFrame to read a CSV file
- Compare running time between Spark and Pandas when reading this large CSV file
- Explore basic Spark DataFrames functionality
- Explore the Spark UI
Requirements: pyspark and Java 17 (see Setup), pandas.
Data: synthetic-cdrs/, synthetic-cell-locs.csv and activity_log_raw.csv in data/raw/.
0. Setup and Configuration
Import required Python packages
Working Directory and Input Files
# ============================================================
# 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
# ============================================================
# Synthetic call detail records (folder of ~10,000 CSV files)
DIR_CDRS = DIR_DATA / "synthetic-cdrs"
# Cell tower locations
FILE_CELL_LOCS = DIR_DATA / "synthetic-cell-locs.csv"
# Activity log (large CSV, 8.3 GB)
FILE_ACTIVITY_LOG = DIR_DATA / "activity_log_raw.csv"Global Variables for Processing
# ============================================================
# SETTINGS
# ============================================================
# None defined yet.Create a SparkSession
As we mentioned during the lecture, there is one main way to interact with Spark and thats using the SparkSession object. Therefore, the very first when writing a Spark application is to instantiate a SparkSession object. A SparkSession can be used create DataFrame, register DataFrame as tables, execute SQL over tables, cache tables, and read parquet files. There can only be one SparkSession per JVM. When creating a SparkSession, there are several options/parameters you can pass. For instance, pass number of cores. We can use * to instruct Spark to use all cores available.
Initialzing Spark
spark = SparkSession.builder \
.master("local[*]") \
.appName("Hello Spark") \
.getOrCreate()Read Data into a Spark DataFrame
We will get into details about the SparkDataFrames API but for now, its enough for you to understand that we can use the SparkSession to read data from different sources such as CSV as shown below.
sdf = spark.read.csv(str(DIR_CDRS), header=True)sim_locs = pd.read_csv(FILE_CELL_LOCS)
sim_locs.head()sdf.columnssim_locs =c# please use the kaggle_expedia_train.csv file here
large_csv = str(FILE_ACTIVITY_LOG)
df = spark.read.csv(large_csv, header=True)Explore Spark UI
Lets run something which takes long (e.g., summary) and then check whats happening on localhost:4040/ in your browser. If localhost:4040/ doesnt work, you can try localhost:4041/
Partitions
You can check and channge number of partitions on the DataFrame
num_partitions = df.rdd.getNumPartitions()
print(num_partitions)
df2 = df.repartition(10)63
Transformations vs. Actions
# Sample() is a transformation and nothing happens you execute the cell
pdf_sample = df.sample(fraction=0.1).toPandas()# Count() is an action because Spark has to actually count
# So, Spark executes the sample first and then do count
df_sample.count()## Example actions: show(), head() are actions
df.show()Compare running time for Spark and Pandas
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_big_csv_with_spark(big_csv=None, spark_session=None):
"""
A simple function which loads a CSV file using Apache Spark and
then counts how many rows are in the file
"""
# create a Spark Session here
# read the CSV file int Spark DataFrame
df = spark_session.read.csv(big_csv, header=True)
# Get the number of rows in the dataset using the count() function
cnt = df.count()
print('Number of rows in big CSV file: {:,}'.format(cnt))@timefn
def load_big_csv_with_pandas(big_csv=None):
"""
Use pandas library to load the large CSV
"""
# Read CSV as a pandas DataFrame (df) here
df = pd.read_csv(big_csv)
# Get the total number of rows
cnt = df.shape[0]
print('Number of rows in big CSV file: {:,}'.format(cnt))EXERCISE-1: Compare running time for spark and pandas.
# Now call the two functions above here
#load_big_csv_with_pandas(big_csv=large_csv)
load_big_csv_with_spark(big_csv=large_csv, spark_session=spark)Exploring DataFrames in Spark
We can do the similar things we did with pandas dataframes in order to explore the data.
- We can use the
printSchema()function to check column data types as well as what kind of columns we have. - Also, you can use the the same
columnsfunction to get a list of all columns - We can use
head()function just like in pandas to get the topnobservations from the data. Note that you can use n within the brackets in the head function to specify the number of rows you want to see. - Count the number of rows in the dataframe using the
count()function - Get number of unique elements using
distinct()command. If you want number of unique elements based on a single column. You can first select the column using the sysntaxdf.select("column").distinct().count()
EXERCISE-2: Exploring spark dataframe
- Use
head()function to view the first 5 observations - Check column data types
- How many unique categories are there for the STATUS variable?
- Using the documentation for Pyspark DataFrame find a function to get a sample of the data. Get a sample of 10% of the data and convert it to a pandas dataframe using
toPandas()function
Although there is a pandas API for spark, we will not get into just yet, lets explore core functionality of spark first
Explore Spark UI
In order to explore the Spark UI, we will run a function that take time to run. For this, lets run the summary() funciton. Once you call the function, open the spakr UI by going to this url: localhost/4040. Once there, explore the different tabs:
- Jobs: On the Jobs tab, you can check the status of your current job. You can see the exact functions such as count that Spark is running
- Executors: In this tab, you can check how cores is Spark using and how tasks are being run in each core
- Stages:. You can at which stage the job is running, how many tasks have completed
- Environment: Its important to see which environment variables Spark is using, you can check that using this tab.
- Storage, SQL: Explore these tabs to see what information they contain
df = spark.read.csv(large_csv, header=True)If the summary job is taking too long, you can kill it using the kill function in Spark UI because in some cases, its impossible to stop it in the notebook.
Other ways to run spark
- spark-shell: For quick and faster interaction with spark.
- spark-submit. In the terminal, often for submitting jobs in clusters but you can also use it in local mode.
EXERCISE-3: Run spark in shell
- Locate spark folder using step-1 below.
- Navigate to the
bindirectory - Run the
pysparkcommand in there
EXERCISE-4: Running spark using spark-submit
Running spark using spark-submit and comparing Spark Running time based on the number of executors assigned to Spark. Unfortunatel, in Jupyter notebook, setting the number of executors isnt working well and so we will have to do it in terminal to explore this.
Step-1: Identify location of your Spark installation.
- Run code below to note down the base folder of you Spark
- Make sure you identify the root of the Spark folder
- On the terminal, navigate to that folder using cd and then navigate to the
binfolder - If you run the
lscommand while in thebinfolder, you should see aspark-submitexecutable spark-submitis used to submit standalone Spark applications to a cluster but we can use it in local mode too
# Run code below to note down the base folder of you Spark
# Make sure you identify the root of the Spark folder
import pysparkpyspark.__file__Step-2: Create a .py file in VS Code or any text editor
- Copy all the necessary imports and add them at the top of the Python file
- Next, add this line of code as its always required when running Python scripts:
if __name__ == "__main__": - Copy the code from just after this heading:
Compare running time for Spark and Pandasto before the heading:Exploring DataFrames in Spark. And paste the code underneath the statement above. - Make sure your Python file has no errors
Step-3: Run the Spark Application
- Note the full path of your Python file
- On the terminal, navigate to the Spark
cd - Within the Spark folder, run this command:
./bin/spark-submit --name "Hello Spark" --master local[num_executors] --conf spark.eventLog.enabled=false --conf "spark.executor.extraJavaOptions=-XX:+PrintGCDetails -XX:+PrintGCTimeStamps" --conf spark.hadoop.abc.def=xyz --conf spark.hive.abc=xyz path_to_your_python_file - To avoid errors, copy the command above into a text editor so that everything is on one line
- Replace
num_executorswith a number such as4for a start. Press enter to run the program. - As the program runs, take note how many executors are created, note the running time for Spark funciton only
- Now, increase the
num_executorsby 2 or 4 and run the program again. See if you notice reduction in running time.
Summary
- TODO
- TODO
- TODO
Next: Spark APIs