我是scala和casbah的新手。我正在尝试使用casbah创建一个mongo replicaset连接。这是我的代码。我很确定我的mongo副本设置是正确的。当我通过ruby创建连接时,它工作得很好。我在这里错过了一些傻事。 当我用Google搜索时,我得到了这个文档,这是我用来参考的。
http://api.mongodb.org/scala/casbah/current/scaladoc/com/mongodb/casbah/MongoConnection $。HTML
import com.mongodb.casbah.Imports._
object MongoAnalysisDB {
def main(args: Array[String]) = {
//that connection
val addresses = List("127.0.0.1:27018", "127.0.0.1:27019", "127.0.0.1:27020")
val mongoConn = MongoConnection(replicaSetSeeds: addresses)
val mongoDB = mongoConn("vimana-sandbox-dup")
val mongoColl = mongoConn("vimana-sandbox-dup")("utilization.metrics.cycledowntime")
//that query
val loadEvent = MongoDBObject("period" -> "PT1H")
val cursor = mongoColl.find(loadEvent)
val mtcevent = mongoColl.findOne(loadEvent)
//that document
println(mtcevent)
}
}
我收到以下错误。
[info] Compiling 1 Scala source to /home/deepak/scala-mongo-oplog-watcher/target/scala-2.9.1/classes...
[error] /home/deepak/scala-mongo-oplog-watcher/src/main/scala/reader.scala:6: ')' expected but '(' found.
[error] val mongoConn = MongoConnection(replicaSetSeeds: List("127.0.0.1:27018", "127.0.0.1:27019", "127.0.0.1:27020"))
[error] ^
[error] /home/deepak/scala-mongo-oplog-watcher/src/main/scala/reader.scala:6: ';' expected but ')' found.
[error] val mongoConn = MongoConnection(replicaSetSeeds: List("127.0.0.1:27018", "127.0.0.1:27019", "127.0.0.1:27020"))
[error] ^
[error] two errors found
[error] {file:/home/deepak/scala-mongo-oplog-watcher/}default-b16d47/compile:compile: Compilation failed
答案 0 :(得分:3)
将ip字符串和端口包装到ServerAddress
工作。
import com.mongodb._
import com.mongodb.casbah.Imports._
object MongoAnalysisDB {
def main(args: Array[String]) = {
//that connection
val addresses = List(new ServerAddress("127.0.0.1" , 27018), new ServerAddress("127.0.0.1" , 27019), new ServerAddress( "127.0.0.1" , 27020 ))
val mongoConn = MongoConnection(addresses)
val mongoDB = mongoConn("vimana-sandbox-dup")
val mongoColl = mongoConn("vimana-sandbox-dup")("utilization.metrics.cycledowntime")
//that query
val loadEvent = MongoDBObject("period" -> "PT1H")
val cursor = mongoColl.find(loadEvent)
val mtcevent = mongoColl.findOne(loadEvent)
//that document
println(mtcevent)
}
}
另见: http://mongodb.github.io/casbah/3.1/reference/connecting/#connecting-to-replicasets-mongos
答案 1 :(得分:1)
您还可以使用MongoClientURI
使用RS连接val mongoClient = MongoClient(MongoClientURI("mongodb://localhost:27018,localhost:27019,localhost:27020"))