This repository contains steps to set up a Spark cluster locally, enabling easy prototyping and development without requiring a distributed environment.
start-cluster.sh: Script to start the Spark cluster.spark_apps/: Directory to place your Spark application scripts.input_data/: Directory to place your input data files.Makefile: Contains commands to manage the Spark cluster and submit applications.
-
Install Dependencies Ensure you have Java and Spark installed on your machine. You can follow the official installation guides for Java and Spark.
-
Start the Spark Cluster
./start-cluster.sh
-
Submit a Spark Job
./bin/spark-submit --class org.apache.spark.examples.SparkPi --master local[4] /path/to/examples.jar 100
Here is an example of how to run a simple Spark job:
-
Create a Python Script Create a file named
example.pywith the following content:from pyspark.sql import SparkSession spark = SparkSession.builder.appName("Example").getOrCreate() data = [("Alice", 1), ("Bob", 2), ("Cathy", 3)] df = spark.createDataFrame(data, ["Name", "Value"]) df.show() spark.stop()
-
Run the Script
./bin/spark-submit example.py
This will start a local Spark session, create a DataFrame, and display its content.
To start the Spark cluster using the Makefile, run:
make runThis command will execute the necessary steps to start the cluster.
To start a scaled Spark cluster, use:
make run-scaledThis command will configure and start a Spark cluster with multiple worker nodes.
To submit a Spark application, add your Python script to the spark_apps folder and run:
make submit app=example.pyReplace example.py with the name of your script.
Place your input data files in the input_data folder. If you want to store data in Hadoop, use the path /opt/hadoop/<path>.
-
Start the Cluster
make run
-
Start a Scaled Cluster
make run-scaled
-
Submit a Spark Application
make submit app=example.py
-
Place Data Files Add your data files to the
input_datadirectory for processing.
By following these steps, you can easily manage your local Spark cluster and run Spark applications.