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.
<a id="h-1-create-uber-or-assembly-jar"></a>
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.
<a id="h-2-add-jars-to-a-spark-submit-classpath"></a>
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
spark-submitoptions. - Finally, properties specified in
spark-defaults.conffile.
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.
<a id="h-2-1-add-jars-to-the-classpath-using-jar-option"></a>
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.aistudylabs.WordCountExample
--jars /path/first.jar,/path/second.jar,/path/third.jar
your-application.jar
Alternatively, you can also use SparkContext.addJar()
<a id="h-2-2-adding-all-jars-from-a-folder-to-classpath"></a>
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.aistudylabs.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.
<a id="h-2-3-adding-jars-with-spark-defaults-conf"></a>
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 (:)
<a id="h-2-4-using-sparkconf-properties"></a>
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("AIStudyLabs.com") \
.config("spark.yarn.dist.jars", "/path/first.jar,/path/second.jar") \
.getOrCreate()
<a id="h-3-add-jars-to-spark-driver-classpath"></a>
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 --driver-class-path or --conf spark.driver.extraClassPath
// Adding jars to Spark Driver
spark-submit -- class com.aistudylabs.WordCountExample \
--jars $(echo /path/jars/*.jar | tr ' ' ',') \
--driver-class-path jar-driver.jar
your-application.jar
<a id="h-5-add-jars-to-spark-shell"></a>
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
<a id="h-6-other-options"></a>
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 !!
<a id="h-related-articles"></a>
Related Articles
- What is Spark Streaming Checkpoint?
- Spark Read Multiple CSV Files
- Spark Submit Command Explained with Examples
- How to Submit a Spark Job via Rest API?
- What is Apache Spark Driver?
- What does setMaster(local[*]) mean in Spark
- Spark – Different Types of Issues While Running in Cluster?
- Apache Spark Interview Questions
- How to Check Spark Version
