PySpark — Distributed Broadcast Variable

Spark enables distributed immutable variables which can be shared across cluster efficiently without any function encapsulation and Broadcast variable is the best example of it.

Broadcasting (image-credit: Wikipedia)

The simplest way of using a variable is to mention it in a function/code and pass it along to task with the code, but it can be inefficient in case of large variables such as tables when those needs to be deserialized on worker nodes multiple times. Thus to remedy comes “Broadcast variables”.

Broadcast variables are shared and cached on every node instead of sending them with each tasks, which avoids serialization issue avoiding performance hits in case of large systems.

How can we use this functionality, lets checkout with a simple example.

For our use case we are supplying two broadcast variables — Department name and establishment year, which will be used in our Students data.

Lets generate our example dataset

# Our example dataset
Students Data frame

Our Broadcast variables

# Broadcast Variable
Broadcast variables

Now, lets use the broadcast variable to get data for out Student’s data frame

# Lets use broadcast variable to use Dept info
_new_schema = ["NAME", "DEPT_CODE", "FAV_SUBJECT", "DEPT_NAME", "ESTD"]
Final Data Frame

Default size for Broadcast variable is 4M which can be controlled through spark.broadcast.blockSize parameter.

Current example might not justify the use case of the Broadcast variable, but consider you are dealing with a big machine learning model that needs to be distributed for performance benefit.

This example very well gives you an idea of how to use it.

Checkout iPython Notebook on Github — https://github.com/subhamkharwal/ease-with-apache-spark/blob/master/19_broadcast_variables.ipynb

Checkout my personal blog — https://urlit.me/blog/

Checkout PySpark Medium Series — https://subhamkharwal.medium.com/learnbigdata101-spark-series-940160ff4d30

Wish to Buy me a Coffee: Buy Subham a Coffee

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store