我正在尝试使用Scala API为MongoDB插入文档,但还没有运气。我正在使用API插入数据并从mongo shell中检查它。 Shell表示没有插入文档,但API在完成执行之前不会抛出任何错误。执行完成后,我收到了这个例外:
[cluster-ClusterId{value='n123b2h34vj23h4j2bk2jb34k', description='null'}-cluster0-shard-00-00.server.com:27017] INFO org.mongodb.driver.cluster - Exception in monitor thread while connecting to server cluster0-shard-00-00.server.com:27017
com.mongodb.MongoSocketReadException: Prematurely reached end of stream
at com.mongodb.connection.AsynchronousSocketChannelStream$BasicCompletionHandler.completed(AsynchronousSocketChannelStream.java:215)
at com.mongodb.connection.AsynchronousSocketChannelStream$BasicCompletionHandler.completed(AsynchronousSocketChannelStream.java:201)
at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:126)
at sun.nio.ch.Invoker.invokeDirect(Invoker.java:157)
at sun.nio.ch.UnixAsynchronousSocketChannelImpl.implRead(UnixAsynchronousSocketChannelImpl.java:553)
at sun.nio.ch.AsynchronousSocketChannelImpl.read(AsynchronousSocketChannelImpl.java:276)
at sun.nio.ch.AsynchronousSocketChannelImpl.read(AsynchronousSocketChannelImpl.java:297)
at com.mongodb.connection.AsynchronousSocketChannelStream$BasicCompletionHandler.completed(AsynchronousSocketChannelStream.java:220)
at com.mongodb.connection.AsynchronousSocketChannelStream$BasicCompletionHandler.completed(AsynchronousSocketChannelStream.java:201)
at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:126)
at sun.nio.ch.UnixAsynchronousSocketChannelImpl.finishRead(UnixAsynchronousSocketChannelImpl.java:430)
at sun.nio.ch.UnixAsynchronousSocketChannelImpl.finish(UnixAsynchronousSocketChannelImpl.java:191)
at sun.nio.ch.UnixAsynchronousSocketChannelImpl.onEvent(UnixAsynchronousSocketChannelImpl.java:213)
at sun.nio.ch.KQueuePort$EventHandlerTask.run(KQueuePort.java:301)
at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
at sun.misc.InnocuousThread.run(InnocuousThread.java:74)
以下是我插入数据的代码段:
val mongoClient = MongoConfig.getMongoClient()
val mongoDatabase = MongoConfig.getMongoDb(mongoClient, "mydb")
val mongoCollection = mongoDatabase.getCollection("mycol")
val userDocument = Document(createBsonDocument(user))
val insertObservable = mongoCollection.insertOne(userDocument)
insertObservable.subscribe(new Observer[Completed] {
override def onNext(result: Completed): Unit = println("Insert operation successful")
override def onError(e: Throwable): Unit = println("Insert operation failed")
override def onComplete(): Unit = println("Insert operation completed")
})
和DB连接的配置: 包some.package
import com.mongodb.MongoCredential
import org.mongodb.scala.{MongoClient, MongoClientSettings, MongoDatabase, MongoCollection, ServerAddress}
import org.mongodb.scala.connection.ClusterSettings
import org.mongodb.scala.bson.collection.immutable.Document
import com.mongodb.{MongoSocketReadException, MongoSocketException}
import scala.collection.JavaConverters._
object MongoConfig {
def setMongoClusterConfig(): ClusterSettings = {
val mongoClusterSettings: ClusterSettings = ClusterSettings
.builder
.hosts(List(new ServerAddress("cluster0-shard-00-00.server.com:27017"),
new ServerAddress("cluster0-shard-00-01.server.com:27017"),
new ServerAddress("cluster0-shard-00-02.server.com:27017"))
.asJava)
.build
mongoClusterSettings
}
def createMongoCredentials(mongoDbName: String = "mydb",
mongoUsername: String = "username",
mongoPassword: Array[Char] = "password".toCharArray): MongoCredential = {
val mongoCredential: MongoCredential = MongoCredential
.createCredential(mongoUsername, mongoDbName, mongoPassword)
mongoCredential
}
@throws(classOf[MongoSocketReadException])
@throws(classOf[MongoSocketException])
def getMongoClient(mongoClusterSettings: ClusterSettings = setMongoClusterConfig(),
mongoCredential: MongoCredential = createMongoCredentials()): MongoClient = {
val mongoClientSettings: MongoClientSettings = MongoClientSettings
.builder
.clusterSettings(mongoClusterSettings)
.credentialList(List(mongoCredential).asJava)
.build
// System.setProperty("org.mongodb.async.type", "netty")
MongoClient(mongoClientSettings)
}
def getMongoDb(mongoClient: MongoClient,
mongoDbName: String): MongoDatabase = {
getMongoClient().getDatabase(mongoDbName)
}
}
有什么想法吗?