PySpark ML + NLP Workshop


1. Explore Amazon reviews

2. Sentimentalize the reviews

3. Word frequency by helpfulness

Workshop Resources

Azure Notebooks Library

Sentiment Notebook

Commoners Notebook

More information

Datasets  | Amazon reviews for NLP | +/- Effect Lexicon

Packages | Spark Package for NLP | Spark ML guide – focus on DataFrame based, NOT RDD-based



Intro to PySpark Workshop 2018-01-24

In this Intro to PySpark Workshop, there are five main points:

  1. About Apache Spark
  2. Sample PySpark Application walkthrough with explanations
  3. Custom built Jupyter Azure Notebook to interactively demonstrate fundamental PySpark concepts
  4. Python-specific Spark advice
  5. Curated resources to learn more


PDF Version: Intro to PySpark Workshop

Q&A Options:

Twitter: #PySparkWorkshop

Sample app

from pyspark.sql import SparkSession
# Build SparkSession, gateway to everything Spark 2.x
spark = SparkSession.builder.appName(name="PySpark Intro").master("local[*]").getOrCreate()

# Create PySpark SQL DataFrame from CSV 
# inferring schema from file
# and using header
green_trips =\
    .option("header", "true")\
    .option("inferSchema", "false")\

# Create a view to use as if it were a SQL table

# Run arbitrary SQL to view total revenue by hour
revenue_by_hour = spark.sql("""
SELECT hour(lpep_pickup_datetime), SUM(total_amount) AS total
FROM green_trips
GROUP BY hour(lpep_pickup_datetime)
ORDER BY hour(lpep_pickup_datetime) ASC""")

# Write out to 25 files (because of 25 partitions) in a directory

This code can be put into a .py file and run using spark-submit at the command line:

> spark-submit

UPDATE: The content for this workshop was live streamed and recorded for PyLadies Remote which can be viewed here

Resources to learn more

Advice for vetting Spark resources

Newer content is generally much more relevant due to the rapid pace that Apache Spark has been developed. Avoid most content before July 2016 which is when Spark 2.0 was released because it may not reflect many critical changes to Spark (such as Data structure APIs like DataFrames/Datasets, Structured Streaming, SparkSession, etc). Content that revolves around Spark 1.x (e.g. Spark 1.6.3) should be avoided as it’s effectively obsolete (ie: last release on 1.x line was Nov ’16 while Spark 2.x has had 6 releases since then). Databricks is essentially a commercial offshoot of the original project at UC Berkeley AMPLab, has Matei Zaharia, the original author of Spark as a co-founder, and employs the majority of Spark contributors. Basically, if Databricks says something about Spark, it would be a good idea to listen.


Learning PySpark (Feb 2017) by Tomasz Drabas and Denny Lee 

Gentle Introduction to Spark by Databricks

Mastering Apache Spark 2 by Jacek Laskowski – note this is more of a dense, incredibly useful reference than a tutorial or book meant to be read linearly

High Performance Spark: Best Practices for Scaling and Optimizing Apache Spark by Holden Karau and Rachel Warren – highly recommended once you’re more comfortable with Spark

Articles and Blog Posts

Introducing Vectorized UDFs for PySpark by Databricks

Jump Start with Apache Spark 2.0 on Databricks by Databricks

Scaling Python for Data Science using Spark by Garren Staubli (me)


Intro to Apache Spark on Databricks by Databricks

Jupyter Azure Notebook by Garren Staubli (me)


Spark: The Definitive Guide (WIP) by Bill Chambers and Matei Zaharia (Databricks)


Extending Spark ML (+ 2nd video) by Holden Karau

Performance Optimization of Recommendation Training Pipeline at Netflix by DB Tsai

Free notebooks

Jupyter notebook in the Microsoft Azure cloud: Azure Notebooks

Databricks community edition

Docker image for Jupyter + PySpark


Scaling Python for Data Science using Spark

Python is the de facto language of Data Science & Engineering. (IMHO R is grand for statisticians, but Python is for the rest of us.)

As a prominent language in the field, it only makes sense that Apache Spark supports it with Python specific APIs. Spark makes it so easy to use Python  that it can actually run slow (up to 30-40x slower than it should) due to naive missteps.

Remember: Spark is written in Scala primarily to leverage JVM performance characteristics, but also has APIs in Python, Java, and R

Even with the great (and rapidly growing!) support for Python on Spark (henceforth PySpark), there are some critical topics that need to be addressed. Do any of these packages look familiar, Python devs?

pandas (aka "pd")
numpy (aka "np")

Thought so. 🙂

Python is fantastic for Initial and Exploratory Data Analysis (IDA/EDA) plus training models and all sorts of other awesome stuff. However, it can’t scale to larger data sizes/parameters due to 2 primary issues:

1) it’s natively single-threaded* due to global interpreter lock (GIL) – out of scope for this post

2) It is not distributed** to multiple machines, so even if it were multi-threaded, it is still bound by one machine’s resources (CPU cores + memory + I/O + network)

* yes, there are different implementations of Python (CPython, Jython, Pypy, etc) that may or may not have mult-threaded/concurrency supported. We’re focusing on the default and predominantly used CPython implementation

** It can be distributed with a hot [relatively] new package “dask” that came out in 2014. We’re sticking with PySpark for this post, but the more you know… AWS does offer a single node (x1e.32xlarge) with 128 vCPUs and ~3.9TB of RAM. If you have to ask how much it costs, you can’t afford it. 😉

No, don’t use RDDs; use [Spark] SQL!

When people think of Spark, one of the first things that comes to mind is the age-old RDD. It is after all the original data structure API that brought Spark to prominence. RDDs still underpin Spark, however since version 2.0, the preferred data structure API for Python has become DataFrames (corollary in Scala/Java is the Datasets API)

DataFrames are under the [massive] umbrella that is “Spark SQL.” DataFrames are preferred to RDDs for two optimization-related reasons; the Catalyst Optimizer and Tungsten. The short explanation of what Catalyst and Tungsten do for DataFrames is automatically handling optimizations that were too low-level for RDDs. The higher level Spark SQL abstractions afforded these APIs with more introspection and greater execution flexibility, thus increasing performance for DataFrames substantially over RDDs. For PySpark users, the performance difference is even greater than for Scala/Java Spark users, because actions done using PySpark SQL APIs directly (e.g. trimming a string, summing an aggregate, regex, etc) are actually executed in the JVM directly rather than in a Python subprocess like RDDs require.

When you’re using DataFrames, be sure to use the baked-in Spark SQL functions (also compatible with hive functions), because they all run optimized in the JVM:

Moral of the story: If you’re using RDDs (especially with PySpark!), evaluate and try to transition to DataFrames. If you’re not using RDDs yet, try DataFrames first.

Note: Dataset is the Scala and Java API that is <em>strongly typed.
</em>DataFrame is literally an alias for Dataset[Row] in Scala
DataFrame does not exist in Java (except as Dataset[Row])

*DataFrame is the only Spark SQL Data Structure API for Python, 
because Python is dynamically typed.*

This section wouldn’t be complete without a silly [but truthful] benchmark:

%%time # RDD
lines = sc.textFile("yellow_tripdata_2017-06.csv")
lineLengths = s: len(s))
totalLength = lineLengths.reduce(lambda a, b: a + b)
print(totalLength) # =&gt; 832591506

CPU times: user 9.66 ms, sys: 2.99 ms, total: 12.6 ms
Wall time: <strong>7.7 s</strong>

%%time # DataFrame
linesDF ="yellow_tripdata_2017-06.csv")
    "sum(length(value)) as slen")
) # =&gt; 832591506

CPU times: user 4.76 ms, sys: 1.74 ms, total: 6.5 ms
Wall time: <strong>3.84 s</strong>

All hope is not lost on “strong typing”-like features for PySpark

Python being dynamically typed is a double edged sword, because by being dynamically typed (therefore forced to only use DataFrames, not Datasets), PySpark DataFrames also natively support accessing variables/columns by name directly. For example…

linesDF =
  .option("header", "true")
    linesDF.passenger_count # &lt;---- HERE

I want to use [insert awesome package here] with PySpark

You probably can, but just because you can doesn’t mean you should.

What I mean by that is when you start mixing Python native code (such as Pandas) with Spark, two bad things happen [usually]. Firstly, your performance nose dives, because as I alluded to briefly above,  any code that runs natively in Python (as opposed to using the Spark SQL APIs) requires [de]serialization to/from Python/JVM and execution in Python subprocesses. More info here, which is a bit old by Spark standards, but any Spark-related wisdom by Holden Karau is great.

The Spark SQL DataFrame API only goes so far (it goes very far FWIW). So maybe you’re tempted to write a UDF (User Defined Function) to extend Spark’s functionality for your use case.

Before you write a UDF that uses Python-specific APIs (not from PySpark), have a look at this simple example and its implications.


We want to convert timestamps in a column to seconds since epoch and count the distinct outputs (yes, this part is purely for benchmarking).

Using UDFs with Python-specific APIs
timestamp_to_epoch = F.udf(lambda t: int(t.strftime("%s")))
CPU times: user 44.8 ms, sys: 18.3 ms, total: 63.1 ms
Wall time: <span style="text-decoration: underline;"><strong>10min 10s</strong></span>
Out[46]: 2340959
Using PySpark SQL APIs
CPU times: user 2.67 ms, sys: 1.15 ms, total: 3.82 ms
Wall time: <span style="text-decoration: underline;"><strong>16.5 s</strong></span>
Out[47]: 2340959

Yes, you read that right. PySpark SQL APIs are 30-40 TIMES FASTER (!!!)

UDF with Python-Specific APIs: 610 seconds
PySpark SQL API: 17 seconds

Fortunately for you, Spark 2.3 solves the issue – partly – by introducing an impressive feature allowing vectorization of Python UDFS, which you can read about more here. Vectorized UDFs for PySpark can again massively improve performance.

I’m a sucker for contrived benchmarks that make my case so clearly. 😀

Here’s a naïve way to build a Spark DataFrame using Pandas first. Note this not only takes over 4 minutes to run, it also uses ~3GB of memory (!!).

pdf = pd.read_table("yellow_tripdata_2017-06.csv")
CPU times: user 4min 2s, sys: 3.33 s, total: 4min 6s
Wall time: <strong>4min 10s</strong>

On the contrary, the same line count using the Spark SQL APIs directly (5 seconds, negligible memory impact):

CPU times: user 1.63 ms, sys: 1.15 ms, total: 2.77 ms
Wall time: <strong>4.57 s</strong>

If you’re going to use Python-native packages with Spark, be mindful of the [unintended] consequences, particularly as it relates to your jobs’ performance.

Special note about Backwards Compatibility

For minor version changes, such as 2.1 to 2.3, many APIs are backwards compatible. Major version changes, such as 1.6 to 2.0 should definitely anticipate backwards incompatible changes. Structured Streaming did go through some fairly significant changes from 2.0.0 to 2.2.0 when it transitioned from experimental to production-ready respectively.

Special Thanks

Thank you Wes Hoffman, Nikki Haas and Eric Lambert for your feedback and suggestions on relevant topics!

Further reading:

A Tale of Three Apache Spark APIs: RDDs, DataFrames, and Datasets

Introducing Vectorized UDFs for PySpark