Spark Set JVM Options to Driver & Executors

Spread the love

How to submit JVM options to Driver and Executors while submitting Spark or PySpark applications via spark-submit.

You can set the JVM options to driver and executors by using spark.driver.extraJavaOptions and spark.executor.extraJavaOptions respectively when using spark-submit

Related: How to set Environment Variables to Executors

By Using Spark Submit

Regardless you are using Spark with Scala or PySpark you can use the extraJavaOptions to set JVM options to driver and executors.


spark-submit --master yarn \
    --deploy-mode cluster \
    --name my-app \
    --conf 'spark.executor.extraJavaOptions=-DenvVar1=var1Value -DenvVar2=var2Value' \
    --conf 'spark.driver.extraJavaOptions=-DenvVar1=var1Value -DenvVar2=var2Value'
    ........
    ........

Using SparkConf

You can also set the JVM options to driver and executors at the time of creating SparkSession. The below example demonstrates with Scala, similarly you can also achieve this in PySpark


import org.apache.spark.sql.SparkSession

// Create SparkSession in spark 2.x or later
val spark = SparkSession.builder().master("local[*]")
    .appName("SparkByExamples.com")
    .conf("spark.driver.extraJavaOptions","-DenvVar1=var1Value")
    .conf("spark.executor.extraJavaOptions","-DenvVar1=var1Value")
    .getOrCreate()

Note that when you submit your Spark or PySpark application in client mode, the spark driver runs on the server where you submit your application.

Note: In client mode, spark.driver.extraJavaOptions config must not be set through the SparkConf (using .conf()) directly in your application, because the driver JVM has already started at that point. Instead, please set this through the --driver-java-options command line option or in your default properties file.


spark-submit --master yarn \
    --deploy-mode client \
    --name my-app \
    --driver-java-options "-DenvVar1=var1Value"
    --conf 'spark.executor.extraJavaOptions=-DenvVar1=var1Value' \
    ........
    ........

Conclusion

In this article, you have learned how to submit JVM arguments or options to Spark or PySpark Driver and Executors by using spark.driver.extraJavaOptions and spark.executor.extraJavaOptions respectively. 

Related Articles

Naveen (NNK)

I am Naveen (NNK) working as a Principal Engineer. I am a seasoned Apache Spark Engineer with a passion for harnessing the power of big data and distributed computing to drive innovation and deliver data-driven insights. I love to design, optimize, and managing Apache Spark-based solutions that transform raw data into actionable intelligence. I am also passion about sharing my knowledge in Apache Spark, Hive, PySpark, R etc.

Leave a Reply

You are currently viewing Spark Set JVM Options to Driver & Executors