I'm using a Databricks Scala notebook with Spark to connect to MongoDB and I'm trying to understand why I'm getting this error when I try to connect to my MongoDB cluster. I simply want to able to read my from database but I'm not sure why this error keeps coming up.
java.lang.NoClassDefFoundError: Could not initialize class com.mongodb.spark.config.ReadConfig$
My code where I'm attempting to read from MongoDB is shown here.
import org.apache.log4j.{Level, Logger}
import org.apache.spark.ml.evaluation.RegressionEvaluator
import org.apache.spark.ml.recommendation.ALS
import org.apache.spark.ml.tuning.{ParamGridBuilder, TrainValidationSplit}
import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkConf, SparkContext}
import com.mongodb.spark.MongoSpark
import com.mongodb.spark.config.{ReadConfig, WriteConfig}
import com.mongodb.spark._
import com.mongodb.spark.config._
val data = spark.read.format("com.mongodb.spark.sql.DefaultSource").option("database", "sample_airbnb").option("collection", "listingsAndReviews").load()
data.show()
I've also installed the following libraries in my notebook library
org.mongodb.spark:mongo-spark-connector_2.12:2.4.0
mongodb_driver_3_12_3_javadoc.jar
mongodb_driver_3_12_3_javadoc.jar
bson_3_12_3_javadoc.jar
These are the uri used for the spark config
spark.mongodb.input.uri mongodb+srv://<user>:<password>@cluster0-ofrzm.azure.mongodb.net/test?retryWrites=true&w=majority
spark.mongodb.output.uri mongodb+srv://<user>:<password>@cluster0-ofrzm.azure.mongodb.net/test?retryWrites=true&w=majority
spark.databricks.delta.preview.enabled true
Any help is greatly appreciated!