SPARK

Command Line

Spark provides two main utilities :

  • the first (spark-submit) is used to submit an application to run on the cluster
  • the second is use to get an interactive shell in either scala (using spark-shell) or python (using pyspark). It cand be used for one-off task or for prototyping.

Both utilities support the same command-line options.

Command Operation
spark-submit --master yarn --deploy-mode cluster Submits a Spark application on the cluster
Option Description
--master Either yarn to start the application on the cluster or local[<number of threads>] to start the application locally (for prototyping purposes mainly)
--deploy-mode when --master is yarn, deploy-mode is either cluster or client. With client, the Spark driver(which handles task scheduling and would typically run your main function) runs locally while the rest of the application runs on the cluster. With cluster, the driver also runs within the cluster with the rest of the application. With this mode, the local process only monitors the state of the application and can be stopped without stoping the application. The cluster deploy-mode is not available for the interactive utilities( spark-shell and pyspark)
--class Name of the main class of the application
--jars comma-separated list of additional local jar files to ship with the application jar
--py-files comma-separated list of additional local python files
--driver-memory Memory for the driver container
--driver-cores Number of cores used by the driver
--executor-memory Memory for executor containers
--executor-cores Number of cores used by each executor container
--num-executors Number of executor containers to start
--conf <key=value> set a spark configuration for the application

Using version 4.3.0

A more recent version of spark (4.3.0) is available. To use this version, use the utilities (spark-submit,spark-shell or pyspark) located in /usr/local/spark-4.3.0-bin-without-hadoop/bin/. This version will use Java version 17 and Python version 3.12 by default.

PYTHON

Using a different environment

It is possible to use a different python environment than the default python installed on the system. The environment must be installed in the mount point /mnt/shared. This mount point is available on all compute nodes of the platform.

It is recommended to first create a directory for your environments (for example mkdir -p /mnt/shared/${USER}/envs) and create your environments there.

Creating your environment

venv

To create your environment, you can use venv.

For example :

python3.12 -m venv /mnt/shared/${USER}/envs/environment_name
source /mnt/shared/${USER}/envs/environment_name/bin/activate

You can then install new packages using pip. For example : pip install scipy.

Once all desired package are installed, you can revert your setup to use the default python with the deactivate command.

conda

Conda is a another way to create python environment that is available on the platform.

To use Conda to create your environment, you must first configure your shell with the following command :

/mnt/shared/public/miniforge/bin/conda init

The command will configure your shell so that Conda is available as the conda command. For the configuration change to be effective, you must logout and relogin of the platform gateway.

One relogged in, you can create your environments like the following example:

conda create -p /mnt/shared/${USER}/envs/environment_name python=3.12
conda activate /mnt/shared/${USER}/envs/environment_name

You can then install packages either from conda-forge using conda : conda install scipy or from PyPI using pip: pip install scipy

Once all desired package are installed, you can revert your setup to use the default python with the command conda deactivate.

Version Compatibility

The default Spark version requires at least version 3.7 and the newer version 4.3.0 requires at least python version 3.11.

Using your environment with Spark

export PYSPARK_PYTHON=/mnt/shared/${USER}/envs/environment_name/bin/python
spark-submit 
pyspark

or preface the command with the environement varaible definition:

PYSPARK_PYTHON=/mnt/shared/${USER}/envs/environment_name/bin/python spark-submit 
PYSPARK_PYTHON=/mnt/shared/${USER}/envs/environment_name/bin/python pyspark

Python Memory

Memory usage is limited by YARN (Hadoop's resource management system) with regards to the amount of resources requested. When using spark, the amount of memory requested is controlled by the option --driver-memory for the driver (controller) and --executor-memory for the executors(computation). This memory includes cached RDDs(datasets), memory used to execute your (Java or Scala ) code as well as Spark internal fonctions.

The configuration options spark.driver.memoryOverhead and spark.executor.memoryOverhead are added to the amount specified with the previous options. When using java(or scala), these overhead account for native VM overhead, interned strings etc.

When using python, these overhead also include memory used by spawned python processes that execute your code. With python, the memory specified with the options --driver-memory and --executor-memory is used by spark to store persisted RDDs, for shuflling data...

As a consequence, when using python, be sure to set a large enough overhead (using for example --conf spark.driver.memoryOverhead=1G --conf spark.executor.memoryOverhead=1G) for your python tasks to avoid a cancellation of your tasks by YARN because of resource overuse. This cancellation results in erros in Spark such as Container killed by YARN for exceeding memory limits. 2.0 GB of 2 GB physical memory used. Consider boosting spark.yarn.executor.memoryOverhead

Projects archetype

Basic project example are available on gitub.u-bordeaux.fr to ease the creation of a new project. This projects contain some very basic examples of use of Spark's APIs.

Language Project URL
Scala https://gitub.u-bordeaux.fr/flalanne/spark_scala_project
Java https://gitub.u-bordeaux.fr/flalanne/spark_java_project
Python https://gitub.u-bordeaux.fr/flalanne/spark_python_project