When submitting Spark or PySpark applications using spark-submit, we often need to include multiple third-party jars in the classpath, Spark supports multiple ways to add dependency jars to the classpath.
For example, to add single or multiple jars to the classpath of the spark application, you can use the –jars option of the spark-submit command, SparkConf, and spark-defaults.conf file.
1. Create uber or assembly jar
Create an assembly or uber jar by including your application classes and all third-party dependencies. You can do this either using the Maven shade plugin or equivalent SBT assembly, for PySpark create a zip file or egg file.
By doing this, you don’t have to worry about adding jars to the classpath as all dependencies are already part of your Uber jar.
2. Add jars to a Spark Submit classpath
Adding multiple third-party jars to classpath can be done using spark-submit,
spark-defaults.conf, and SparkConf properties, before using these options you need to understand the priority of how these apply. Below is the precedence of how they apply in order.
- Properties set directly on the
SparkConftake the highest precedence.
- The second precedence goes to
- Finally, properties specified in
When you are setting jars in different places, remember the precedence it takes. Use spark-submit with
--verbose option to get more details about what jars Spark has used.
2.1 Add jars to the classpath using –jar Option
You can add jars using Spark submit option
--jar, using this option you can add a single jar or multiple jars by comma-separated.
// Adding jars to the classpath spark-submit --master yarn --class com.sparkbyexamples.WordCountExample --jars /path/first.jar,/path/second.jar,/path/third.jar your-application.jar
Alternatively, you can also use
2.2 Adding all jars from a folder to classpath
You can use the below snippet to add all jars from a folder automatically. Here,
$(echo /path/*.jar | tr ' ' ',') statement creates a comma-separated string by appending all jar names in a folder.
// Adding all jars from a folder to classpath spark-submit -- class com.sparkbyexamples.WordCountExample \ --jars $(echo /path/*.jar | tr ' ' ',') \ your-application.jar
This option is handy when you have many jars to append to the classpath. Imagine using tens of jars in a comma-separated manually and when you have to update the version of the jars, it’s going to be a nightmare to maintain. Hence, place all required jars in a folder/directory and add them to Spark classpath using the above approach.
2.3 Adding jars with spark-defaults.conf
You can also specify jars on
$SPARK_HOME/conf/spark-defaults.conf, but this is not a preferable option and any libraries you specify here take low precedence.
// Add jars to driver classpath spark.driver.extraClassPath /path/first.jar:/path/second.jar // Add jars to executor classpath spark.executor.extraClassPath /path/first.jar:/path/second.jar
Note that on Windows, the jar file names should be separated with comma (,) instead of colon (:)
2.4 Using SparkConf properties
You can also programmatically add multiple jars to the Spark classpath while creating the SparkSession. This takes high priority among other configs.
// Using SparkConf properties spark = SparkSession \ .builder \ .appName("SparkByExamples.com") \ .config("spark.yarn.dist.jars", "/path/first.jar,/path/second.jar") \ .getOrCreate()
3. Add jars to Spark Driver Classpath
Sometimes you may need to add a jar to only the Spark driver, you can do this by using
// Adding jars to Spark Driver spark-submit -- class com.sparkbyexamples.WordCountExample \ --jars $(echo /path/jars/*.jar | tr ' ' ',') \ --driver-class-path jar-driver.jar your-application.jar
5. Add jars to spark-shell
Options on spark-shell are similar to spark-submit hence you can use the options specified above to add one or multiple jars to spark-shell classpath.
// Add jars to spark-shell spark-shell --driver-class-path /path/to/example.jar:/path/to/another.jar
6. Other options
You can also set jars to Spark driver classpath using the below options.
--conf spark.driver.extraLibraryPath=/path/ // Or use below, both do the same --driver-library-path /path/
Happy Learning !!