1

我是 scala 和 casbah 的新手。我正在尝试使用 casbah 创建一个 mongo 副本集连接。这是我的代码。我很确定我的 mongo 副本设置是正确的。当我通过 ruby​​ 创建连接时,效果很好。我在这里错过了一些愚蠢的东西。当我用谷歌搜索时,我得到了这个文档,这是我用来参考的。

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
4

2 回答 2

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

于 2012-05-15T10:11:13.373 回答
1

您还可以使用 MongoClientURI 使用 RS 连接

val mongoClient = MongoClient(MongoClientURI("mongodb://localhost:27018,localhost:27019,localhost:27020"))
于 2014-10-22T07:53:55.453 回答