简体   繁体   中英

Errors when running my first MongoClient scala code with mongo-scala-driver 2.1.0

I followed the instructions at http://mongodb.github.io/mongo-scala-driver/ to install for scala 2.11. But my first class cannot run.

def main(args: Array[String]): Unit = {
   val mongoClient: MongoClient = if (args.isEmpty) MongoClient() else MongoClient("mongodb://localhost")
}

Exception:

Exception in thread "main" java.lang.NoSuchMethodError: com.mongodb.connection.DefaultClusterFactory.create(Lcom/mongodb/connection/ClusterSettings;Lcom/mongodb/connection/ServerSettings;Lcom/mongodb/connection/ConnectionPoolSettings;Lcom/mongodb/connection/StreamFactory;Lcom/mongodb/connection/StreamFactory;Ljava/util/List;Lcom/mongodb/event/ClusterListener;Lcom/mongodb/event/ConnectionPoolListener;Lcom/mongodb/event/ConnectionListener;Lcom/mongodb/event/CommandListener;Ljava/lang/String;Lcom/mongodb/client/MongoDriverInformation;)Lcom/mongodb/connection/Cluster;
    at com.mongodb.async.client.MongoClients.createMongoClient(MongoClients.java:188)
    at com.mongodb.async.client.MongoClients.create(MongoClients.java:181)
    at com.mongodb.async.client.MongoClients.create(MongoClients.java:123)
    at org.mongodb.scala.MongoClient$.apply(MongoClient.scala:102)
    at org.mongodb.scala.MongoClient$.apply(MongoClient.scala:77)
    at org.mongodb.scala.MongoClient$.apply(MongoClient.scala:51)
    at org.mongodb.scala.MongoClient$.apply(MongoClient.scala:43)
    at db.mongo.client.MongoClientExample$.main(MongoClientExample.scala:18)
    at db.mongo.client.MongoClientExample.main(MongoClientExample.scala)

I have had the same problem and it was due to a wrong dependency to the underlying Java driver. The Mongo Spark connector had a dependency to version 3.2.2 of the Java driver, which apparently was also used by the Scala driver. I solved the problem by defining the dependencies as follows in sbt:

val mongoScalaDriver = "org.mongodb.scala" %% "mongo-scala-driver" % "2.1.0"
  val mongoJavaDriver = "org.mongodb" % "mongo-java-driver" % "3.4.2"
  val mongoSpark = Seq(mongoScalaDriver, mongoJavaDriver, "org.mongodb.spark" %% "mongo-spark-connector" % "2.0.0")

I use then mongoSpark in my library project. Also, I added explicitly a dependency to version 3.4.2 of the Java driver.

我终于在build.sbt的另一行中解决了这个问题:

    libraryDependencies += "org.mongodb.scala" %% "mongo-scala-bson" % "2.1.0"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM