Menu

Install PySpark on MAC – A Step-by-Step Guide to Install PySpark on MAC with Code Examples

Introduction

Apache PySpark is a powerful open-source data processing engine built on the Apache Hadoop ecosystem, used for big data processing and analytics.

In this blog post, we will walk through the process of setting up the environment and installing PySpark on a Mac operating system.

This step-by-step guide will cover prerequisites, installation, and example code to help you get started with PySpark.

Prerequisites

Before we begin the installation, ensure you have the following prerequisites:

  1. A Mac operating system (macOS High Sierra or later).

  2. Homebrew (a package manager for macOS) installed. If not, follow the installation instructions here: https://brew.sh/

  3. Python 3.x installed. If not, you can download it from https://www.python.org/downloads/mac-osx/

1. Install Java Development Kit (JDK)

PySpark requires Java 8 or later to run. To install the latest version of JDK, open your terminal and execute the following command:

brew install openjdk

To check if the installation was successful, run the following command:

java -version

2. Set JAVA_HOME environment variable

Set the JAVA_HOME environment variable in your shell profile (e.g., ~/.bashrc or ~/.zshrc) by adding the following line:

export JAVA_HOME=$(/usr/libexec/java_home)

Then, run the following command to source the changes:

source ~/.bashrc

3. Install Apache Spark

First, we need to install Apache Spark using Homebrew. Open the Terminal and run the following command:

brew install apache-spark

This command will install the latest version of Apache Spark on your macOS system.

4. Set Environment Variables

Next, we need to set the environment variables for PySpark. Add the following lines to your shell profile (e.g., ~/.bash_profile, ~/.bashrc, or ~/.zshrc):

export SPARK_HOME=/usr/local/Cellar/apache-spark/<version>/libexec
export PYSPARK_PYTHON=python3
export PYSPARK_DRIVER_PYTHON=python3

Replace version with the installed Spark version (e.g., 3.2.0). Save the file and run the following command to apply the changes:

source ~/.bash_profile

5. Install PySpark Python Package

To use PySpark in your Python projects, you need to install the PySpark package. Run the following command to install PySpark using pip:

pip install pyspark

Verify the Installation
To verify that PySpark is successfully installed and properly configured, run the following command in the Terminal:

pyspark --version

6. Example PySpark Code

Now that PySpark is installed, let’s run a simple example. Create a Python script called “wordcount.py” with the following content:

from pyspark.sql import SparkSession

# Initialize a Spark session
spark = SparkSession.builder \
    .appName("Word Count Example") \
    .getOrCreate()

# Create an RDD from a text file
text_file = spark.sparkContext.textFile("example.txt")

# Perform a word count
word_counts = text_file.flatMap(lambda line: line.split(" ")) \
    .map(lambda word: (word, 1)) \
    .reduceByKey(lambda a, b: a + b)

# Print the word count results
for word, count in word_counts.collect():
    print(f"{word}: {count}")

# Stop the Spark session
spark.stop()

Save the file and create a sample text file called “example.txt” in the same directory with some text. Run the script using the following command:

spark-submit wordcount.py

You should see the word count results in the Terminal.

Conclusion:

In this blog post, we’ve guided you through the process of installing PySpark on macOS and provided an example of PySpark code to get you started. Remember to consider the prerequisites and environment variables during the installation process.

With PySpark now installed, you’re ready to dive into large-scale data processing and analytics using Apache Spark on your macOS system.

Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science