Built with Tutify.io

    AIStudyLabs

    Install Spark on Windows

    Install Spark on Windows

    Install Spark on Windows

    In this lesson, I will explain step-by-step how to do Apache Spark Installation on Windows OS and explain how to start a history server and monitor your jobs using Web UI.


    Note: Please use the latest version. The Java, Scala and Spark versions I used in this lesson might be older depending on when you are using it. I would highly recommend using the latest available versions for each tool.


    Install Java 21 or Later

    To install Apache Spark on Windows, you would need Java 17 or the latest version hence download the Java version from Oracle and install it on your system. If you wanted OpenJDK you can download it from here. You can also download from https://learn.microsoft.com/en-us/java/openjdk/download

    After downloading, double-click on the downloaded .exe (microsoft-jdk-21.0.10-windows-x64) file in order to install it on your Windows system. Choose any custom directory or keep the default location.

    Note: This article explains Installing Apache Spark on Java 21.

    Apache Spark Installation on Windows

    Apache Spark comes in compressed tar/zip files hence installation on Windows is not much of a deal as you just need to download and untar the file. Download Apache Spark by accessing the Spark Download page and select the link from “Download Spark (point 3 from below screenshot)”.

    <img height="99" width="523" src="https://yrltjyzsmzdylqvoonwf.supabase.co/storage/v1/object/public/lesson-images/100/181/spark-download.png" />

    If you want to use a different version of Spark & Hadoop, select the one you wanted from the drop-down; the link on point 3 changes to the selected version and provides you with an updated link to download.

    After download, untar the binary using 7zip or any zip utility to extract the zip file and copy the extracted directory spark-4.1.1-bin-hadoop3-connect to c:\apps\opt\spark-4.1.1-bin-hadoop3-connect

    Spark Environment Variables

    Post Java and Apache Spark installation on Windows, set JAVA_HOME, SPARK_HOME, HADOOP_HOME and PATH environment variables. If you know how to set the environment variable on Windows, add the following.

    JAVA_HOME = C:\Program Files\Java\jdk21
    PATH = %PATH%;%JAVA_HOME%
    SPARK_HOME  = C:\apps\opt\spark-4.1.1-bin-hadoop3-connect
    HADOOP_HOME = C:\apps\opt\spark-4.1.1-bin-hadoop3-connect
    PATH=%PATH%;%SPARK_HOME%
    

    Follow the below steps if you are not aware of how to add or edit environment variables on Windows.

    1. Open the System Environment Variables window and select Environment Variables.
    <img height="410" width="410" alt="Apache Spark Installation windows" src="https://files.cdn.thinkific.com/file_uploads/843504/images/bc5/4eb/f9e/1696397600458.jpg" />

    2. On the following Environment variable screen, add SPARK_HOME, HADOOP_HOME, JAVA_HOME by selecting the New option.

    Note: The below images are with older versions. Make sure you are using the right folder/file names as per your setup.

    <img height="411" width="411" alt="spark installation environment variable" src="https://files.cdn.thinkific.com/file_uploads/843504/images/c39/7d0/107/1696397600080.jpg" />

    3. This opens up the New User Variables window where you can enter the variable name and value.

    4. Now Edit the PATH variable

    <img height="394" width="394" alt="apache spark install windows" src="https://files.cdn.thinkific.com/file_uploads/843504/images/900/2ad/c75/1696397600189.jpg" />

    5. Add Spark, Java, and Hadoop bin location by selecting the New option.

    <img height="452" width="452" alt="spark install windows" src="https://files.cdn.thinkific.com/file_uploads/843504/images/7bf/a24/c32/1696397600226.jpg" />

    Spark with winutils.exe on Windows

    Many beginners think Apache Spark needs a Hadoop cluster installed to run but that’s not true, Spark can run on AWS by using S3, and Azure by using blob storage without Hadoop and HDFSe.t.c.

    To run Apache Spark on Windows, you need winutils.exe as it uses POSIX like file access operations in Windows using Windows API.

    winutils.exe enables Spark to use Windows-specific services including running shell commands on a Windows environment.

    Download winutils.exe for Hadoop 3 and copy it to %SPARK_HOME%\bin folder. Winutils are different for each Hadoop version hence download the right version based on your Spark vs Hadoop distribution from https://github.com/steveloughran/winutils

    Apache Spark shell

    spark-shell is a CLI utility that comes with Apache Spark distribution, open command prompt, go to cd %SPARK_HOME%/bin and type spark-shell command to run Apache Spark shell. You should see something like this below (ignore the error you see at the end). Sometimes it may take a minute or two for your Spark instance to initialize to get to the below screen.

    <img height="341" width="599" src="https://yrltjyzsmzdylqvoonwf.supabase.co/storage/v1/object/public/lesson-images/100/181/spark-shell-v4.png" />

    Spark Shell Command Line

    Spark-shell also creates a Spark context web UI and by default, it can access from http://localhost:4041.

    On the spark-shell command line, you can run any Spark statements like creating an RDD, getting a Spark version etc

    scala> spark.versionres2: String = 3.0.0
    scala> val rdd = sc.parallelize(Array(1,2,3,4,5,6,7,8,9,10))rdd: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[0] at parallelize at console:24scala>
    

    This completes the installation of Apache Spark on Windows.

    Web UI on Windows

    Apache Spark provides a suite of Web UIs (Jobs, Stages, Tasks, Storage, Environment, Executors, and SQL) to monitor the status of your Spark application, resource consumption of the Spark cluster, and Spark configurations. On Spark Web UI, you can see how the operations are executed.

    Spark Web UI

    <img height="239" width="676" src="https://yrltjyzsmzdylqvoonwf.supabase.co/storage/v1/object/public/lesson-images/100/181/spark-web-ui-v4.png" />

    History Server

    The history server keeps a log of all Spark applications you submit by spark-submit, spark-shell. You can enable Spark to collect the logs by adding the below configs to spark-defaults.conf file, conf file is located at %SPARK_HOME%/conf directory.

    spark.eventLog.enabled truespark.history.fs.logDirectory file:///c:/logs/path
    

    After setting the above properties, start the history server by starting the below command.

    $SPARK_HOME/bin/spark-class.cmd org.apache.spark.deploy.history.HistoryServer
    

    By default, the History server listens at the 18080 port and you can access it from the browser using http://localhost:18080/

    By clicking on each App ID, you will get the details of the application in Spark web UI.

    Apache Spark Tutorial with Java Examples — Setup Apache Spark Environment

    Introduction to Apache Spark

    Setup Apache Spark Environment

    SparkSession & SparkContext

    Resilient Distributed Datasets (RDDs)

    Cache, Persistence and Shared Variables

    DataFrame Introduction

    DataFrame Narrow Transformation

    DataFrame Wider Transformation

    DataFrame Actions

    Spark SQL Functions

    Advanced DataFrame Features

    Using SQL Queries

    Spark Read & Write CSV Files

    Working with JSON Files

    Working with Parquet Files

    Spark Dataset

    What Next?

    Tutorial Not Found