Skip to main content

Command Palette

Search for a command to run...

Submitting Spark Application

Updated
โ€ข5 min read
Submitting Spark Application
N

I am a Tech Enthusiast having 13+ years of experience in ๐ˆ๐“ as a ๐‚๐จ๐ง๐ฌ๐ฎ๐ฅ๐ญ๐š๐ง๐ญ, ๐‚๐จ๐ซ๐ฉ๐จ๐ซ๐š๐ญ๐ž ๐“๐ซ๐š๐ข๐ง๐ž๐ซ, ๐Œ๐ž๐ง๐ญ๐จ๐ซ, with 12+ years in training and mentoring in ๐’๐จ๐Ÿ๐ญ๐ฐ๐š๐ซ๐ž ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ๐ข๐ง๐ , ๐ƒ๐š๐ญ๐š ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ๐ข๐ง๐ , ๐“๐ž๐ฌ๐ญ ๐€๐ฎ๐ญ๐จ๐ฆ๐š๐ญ๐ข๐จ๐ง ๐š๐ง๐ ๐ƒ๐š๐ญ๐š ๐’๐œ๐ข๐ž๐ง๐œ๐ž. I have ๐’•๐’“๐’‚๐’Š๐’๐’†๐’… ๐’Ž๐’๐’“๐’† ๐’•๐’‰๐’‚๐’ 10,000+ ๐‘ฐ๐‘ป ๐‘ท๐’“๐’๐’‡๐’†๐’”๐’”๐’Š๐’๐’๐’‚๐’๐’” and ๐’„๐’๐’๐’…๐’–๐’„๐’•๐’†๐’… ๐’Ž๐’๐’“๐’† ๐’•๐’‰๐’‚๐’ 500+ ๐’•๐’“๐’‚๐’Š๐’๐’Š๐’๐’ˆ ๐’”๐’†๐’”๐’”๐’Š๐’๐’๐’” in the areas of ๐’๐จ๐Ÿ๐ญ๐ฐ๐š๐ซ๐ž ๐ƒ๐ž๐ฏ๐ž๐ฅ๐จ๐ฉ๐ฆ๐ž๐ง๐ญ, ๐ƒ๐š๐ญ๐š ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ๐ข๐ง๐ , ๐‚๐ฅ๐จ๐ฎ๐, ๐ƒ๐š๐ญ๐š ๐€๐ง๐š๐ฅ๐ฒ๐ฌ๐ข๐ฌ, ๐ƒ๐š๐ญ๐š ๐•๐ข๐ฌ๐ฎ๐š๐ฅ๐ข๐ณ๐š๐ญ๐ข๐จ๐ง๐ฌ, ๐€๐ซ๐ญ๐ข๐Ÿ๐ข๐œ๐ข๐š๐ฅ ๐ˆ๐ง๐ญ๐ž๐ฅ๐ฅ๐ข๐ ๐ž๐ง๐œ๐ž ๐š๐ง๐ ๐Œ๐š๐œ๐ก๐ข๐ง๐ž ๐‹๐ž๐š๐ซ๐ง๐ข๐ง๐ . I am interested in ๐ฐ๐ซ๐ข๐ญ๐ข๐ง๐  ๐›๐ฅ๐จ๐ ๐ฌ, ๐ฌ๐ก๐š๐ซ๐ข๐ง๐  ๐ญ๐ž๐œ๐ก๐ง๐ข๐œ๐š๐ฅ ๐ค๐ง๐จ๐ฐ๐ฅ๐ž๐๐ ๐ž, ๐ฌ๐จ๐ฅ๐ฏ๐ข๐ง๐  ๐ญ๐ž๐œ๐ก๐ง๐ข๐œ๐š๐ฅ ๐ข๐ฌ๐ฌ๐ฎ๐ž๐ฌ, ๐ซ๐ž๐š๐๐ข๐ง๐  ๐š๐ง๐ ๐ฅ๐ž๐š๐ซ๐ง๐ข๐ง๐  new subjects.

There are different ways to submit your application on a cluster but the most common is to use the spark-submit.

spark-submit

spark-submit is a command-line tool provided by Apache Spark for submitting Spark applications to a cluster. It is used to launch applications on a standalone Spark cluster, a Hadoop YARN cluster, or a Mesos cluster.

The spark-submit tool takes a JAR file or a Python file as input along with the applicationโ€™s configuration options and submits the application to the cluster. The configuration options can be used to set various parameters for the application, such as the number of executor cores, the amount of memory allocated to each executor, and the number of executors.

The general structure of the spark-submit command is as follows:

./bin/spark-submit [options] <application-jar> [application-arguments]
  • spark-submit: The name of the command-line tool for submitting Spark applications.

  • [options]: Optional command-line options that configure the behavior of the spark-submit tool and the Spark application being submitted.

  • <application-jar>: The path to the JAR file containing the Spark application code. This JAR file must be created beforehand using a build tool. (A JAR (Java Archive) file is a package file format used to aggregate Java class files, associated metadata, and resources (such as images, sound files, and other supporting files) into a single file. It is a standard format used for distributing Java applications and libraries.)

  • [application-arguments]: Optional command-line arguments that are passed to the Spark application's main method.

spark-submit options

some of the common options, configurations, and specific options to use with Scala and Python.

./bin/spark-submit --help

Submit Scala Application

./bin/spark-submit \
--master yarn \
--class org.nppntraining.WordCountExample \
user-data-analysis.jar

Submit PySpark Application

./bin/spark-submit \
   --master yarn \
   WordCountExample.py

Commonly used options

Here are some examples of commonly used options:

  • Using --master option, you specify what cluster manager to use to run your application. Spark currently supports Yarn, Mesos, Kubernetes, Stand-alone, and local.

  • --class <main-class>: Specifies the fully qualified name of the main class for the Spark application. This option is required for Java or Scala applications, but not needed for pySpark.

./bin/spark-submit \
    --master yarn \
    --class org.apache.spark.examples.SparkPi \
    /home/npntraining/apache-spark-2.4.0/jars/spark-examples_versionxx.jar 80

Cluster Managers

Using --master option, you specify what cluster manager to use to run your application. Spark currently supports Yarn, Mesos, Kubernetes, Stand-alone, and local.

Deployment Modes

Using --deploy-mode, you specify where to run the Spark application driver program. Spark supports cluster and client deployment modes.

Cluster: In cluster mode, the driver runs on one of the worker nodes, and this node shows as a driver on the Spark Web UI of your application. cluster mode is used to run production jobs.

Client: In client mode, the driver runs locally where you are submitting your application from. in client mode, only the driver runs locally and all other executors run on different nodes on the cluster.

Driver program: This is your bundled jar application that drives the entire application. The Driver talks to an instance of Spark Master to submit your job to the cluster.

Driver and Executor Resources (Cores & Memory)

While submitting an application, you can also specify how much memory and cores you wanted to give for driver and executors.

  • --executor-memory <memory>: Specifies the amount of memory to allocate to each executor in the Spark application. The memory value can be specified in units such as g (gigabytes) or m (megabytes).

  • --executor-cores Number of CPU cores to use for the executor process.

  • --total-executor-coresThe total number of executor cores to use.

  • --driver-memoy Memory to be used by the Spark driver.

  • --driver-cores CPU cores to be used by the Spark driver.

  • --num-executorsThe total number of executors to use.

./bin/spark-submit \
   --master yarn \
   --deploy-mode cluster \
   --driver-memory 8g \
   --executor-memory 16g \
   --executor-cores 2  \
   --class org.apache.spark.examples.SparkPi \
   /home/npntraining/apache-spark-2.4.0/jars/spark-examples_versionxx.jar 80

Other Options

--files: Use the comma-separated files you wanted to use. Usually, these can be files from your resource folder. Using this option, Spark submits all these files to the cluster.

--verbose: Displays the verbose information. For example, writes all configurations the spark application uses to the log file.

Note: Files specified with --files are uploaded to the cluster.

Example: Below example submits the application to yarn cluster manager by using cluster deployment mode and with 16g driver memory, 32g, and 4 cores for each executor.

./bin/spark-submit \
   --verbose
   --master yarn \
   --deploy-mode cluster \
   --driver-memory 16g \
   --executor-memory 32g \
   --executor-cores 4  \
   --files /path/log4j.properties,/path/file2.conf,/path/file3.json
   --class org.apache.spark.examples.SparkPi \
   /home/npntraining/apache-spark-2.4.0/jars/spark-examples_versionxx.jar 80

Spark Submit Configurations

Spark submit supports several configurations using --config, these configurations are used to specify Application configurations, shuffle parameters, runtime configurations.

Configuration KeyDescription
spark.sql.shuffle.partitionsNumber of partitions to create for wider shuffle transformations (joins and aggregations).
spark.executor.memoryOverheadThe amount of additional memory to be allocated per executor process in cluster mode, it is typically memory for JVM overheads. (Not supported for PySpark)
spark.serializerorg.apache.spark.serializer.<br>JavaSerializer (default)

org.apache.spark.serializer.KryoSerializer | | spark.sql.files.maxPartitionBytes | The maximum number of bytes to be used for every partition when reading files. Default 128MB. | | spark.dynamicAllocation.enabled | Specifies whether to dynamically increase or decrease the number of executors based on the workload. Default true. | | spark.dynamicAllocation
.minExecutors | A minimum number of executors to use when dynamic allocation is enabled. | | spark.dynamicAllocation
.maxExecutors | A maximum number of executors to use when dynamic allocation is enabled. | | spark.executor.extraJavaOptions | Specify JVM options (see example below) |

./bin/spark2-submit \
--master yarn \
--deploy-mode cluster \
--conf "spark.sql.shuffle.partitions=20000" \
--conf "spark.executor.memoryOverhead=5244" \
--conf "spark.memory.fraction=0.8" \
--conf "spark.memory.storageFraction=0.2" \
--conf "spark.serializer=org.apache.spark.serializer.KryoSerializer" \
--conf "spark.sql.files.maxPartitionBytes=168435456" \
--conf "spark.dynamicAllocation.minExecutors=1" \
--conf "spark.dynamicAllocation.maxExecutors=200" \
--conf "spark.dynamicAllocation.enabled=true" \
--conf "spark.executor.extraJavaOptions=-XX:+PrintGCDetails -XX:+PrintGCTimeStamps" \ 
--files /path/log4j.properties,/path/file2.conf,/path/file3.json \
--class org.apache.spark.examples.SparkPi \
/home/npntraining/apache-spark-2.4.0/jars/spark-examples_versionxx.jar 80

Reference: http://spark.apache.org/docs/latest/submitting-applications.html


If you like my work connect with me

  1. I share tips, tricks and insights on #softwareengineering, #dataengineering #cloud #ml on LinkedIn.

  2. Do you want to connect with me I have started mentoring others for career and interviews at ๐ญ๐จ๐ฉ๐ฆ๐š๐ญ๐ž.๐ข๐จ/๐ง๐š๐ฏ๐ž๐ž๐ง๐ฉ๐ง

More from this blog

Naveen P.N's Tech Blog

94 posts