尝试连接到 MongoDB 时出现异常 Class 未找到 ConnectionString

While trying to connect to MongoDB got exception Class ConnectionString not found

我正在尝试连接到 MongoDB 来编写一个集合。正确创建了 spark 会话,但是当我尝试将数据插入 Mongo 时,出现错误:

MongoSpark.write(stringPeriodosDataframe).option("collection", "periodos").mode("overwrite").save();

我得到的异常:

Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/ConnectionString
    at com.mongodb.spark.config.MongoCompanionConfig$$anonfun.apply(MongoCompanionConfig.scala:278)
    at com.mongodb.spark.config.MongoCompanionConfig$$anonfun.apply(MongoCompanionConfig.scala:278)
    at scala.util.Try$.apply(Try.scala:192)
    at com.mongodb.spark.config.MongoCompanionConfig$class.connectionString(MongoCompanionConfig.scala:278)
    at com.mongodb.spark.config.WriteConfig$.connectionString(WriteConfig.scala:37)
    at com.mongodb.spark.config.WriteConfig$.apply(WriteConfig.scala:209)
    at com.mongodb.spark.config.WriteConfig$.apply(WriteConfig.scala:37)
    at com.mongodb.spark.config.MongoCompanionConfig$class.apply(MongoCompanionConfig.scala:124)
    at com.mongodb.spark.config.WriteConfig$.apply(WriteConfig.scala:37)
    at com.mongodb.spark.config.MongoCompanionConfig$class.apply(MongoCompanionConfig.scala:113)
    at com.mongodb.spark.config.WriteConfig$.apply(WriteConfig.scala:37)
    at com.mongodb.spark.sql.DefaultSource.createRelation(DefaultSource.scala:81)
    at org.apache.spark.sql.execution.datasources.SaveIntoDataSourceCommand.run(SaveIntoDataSourceCommand.scala:46)
    at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult$lzycompute(commands.scala:70)
    at org.apache.spark.sql.execution.command.ExecutedCommandExec.sideEffectResult(commands.scala:68)
    at org.apache.spark.sql.execution.command.ExecutedCommandExec.doExecute(commands.scala:86)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute.apply(SparkPlan.scala:131)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute.apply(SparkPlan.scala:127)
    at org.apache.spark.sql.execution.SparkPlan$$anonfun$executeQuery.apply(SparkPlan.scala:155)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.sql.execution.SparkPlan.executeQuery(SparkPlan.scala:152)
    at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:127)
    at org.apache.spark.sql.execution.QueryExecution.toRdd$lzycompute(QueryExecution.scala:80)
    at org.apache.spark.sql.execution.QueryExecution.toRdd(QueryExecution.scala:80)
    at org.apache.spark.sql.DataFrameWriter$$anonfun$runCommand.apply(DataFrameWriter.scala:654)
    at org.apache.spark.sql.DataFrameWriter$$anonfun$runCommand.apply(DataFrameWriter.scala:654)
    at org.apache.spark.sql.execution.SQLExecution$.withNewExecutionId(SQLExecution.scala:77)
    at org.apache.spark.sql.DataFrameWriter.runCommand(DataFrameWriter.scala:654)
    at org.apache.spark.sql.DataFrameWriter.saveToV1Source(DataFrameWriter.scala:273)
    at org.apache.spark.sql.DataFrameWriter.save(DataFrameWriter.scala:267)
    at com.santander.espana.CargarPeriodos.insertCollectionPeriodoDataframe(CargarPeriodos.java:117)
    at com.santander.espana.CargarPeriodos.build(CargarPeriodos.java:71)
    at com.santander.espana.MainCargaPeriodos.init(MainCargaPeriodos.java:38)
    at com.santander.espana.Prueba.main(Prueba.java:50)
Caused by: java.lang.ClassNotFoundException: com.mongodb.ConnectionString
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 34 more
Disconnected from the target VM, address: '127.0.0.1:60130', transport: 'socket'

Process finished with exit code 1

版本:

mongo-spark-connector_2.11-2.3.0
Java1.8
IntelliJ 2021 1.2 社区
Spark 库版本 2.11

我正在使用的其他依赖版本:

hadoop 2.7, 火花2.3.0, java 驱动程序 2.7, 火花催化剂,核心,蜂巢,sql ....所有 2.11:2.3.0, scala scala-library:2.11.12

MongoDB 4.0.16

坚持这一点,我们非常欢迎任何帮助

谢谢!

终于在这里提供解决方案:

有效!

我用的是最新版本:mongo-java-driver-3.12.10