当前位置: 首页>>代码示例>>Scala>>正文


Scala MongoClient类代码示例

本文整理汇总了Scala中com.mongodb.casbah.MongoClient的典型用法代码示例。如果您正苦于以下问题:Scala MongoClient类的具体用法?Scala MongoClient怎么用?Scala MongoClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了MongoClient类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: Config

//设置package包名称以及导入依赖的类
package conf.connection

import java.net.InetAddress

import com.datastax.driver.core.Cluster
import com.mongodb.casbah.{MongoDB, MongoClientURI, MongoClient}
import com.typesafe.config.ConfigFactory
import com.websudos.phantom.connectors.{KeySpace, SessionProvider}
import com.websudos.phantom.dsl.Session

import scala.collection.JavaConversions._



object Config {
  val config = ConfigFactory.load()

}

object HashDB {
  def getConnection(): MongoDB={
    val hosts = Config.config.getString("mongodb.host")
    val database = Config.config.getString("mongodb.database")
    val uri = MongoClientURI(hosts)
    MongoClient(uri)(database)
  }
}

trait DefaultsConnector extends SessionProvider {
  val config = ConfigFactory.load()
  implicit val space: KeySpace = DataConnection.keySpace
  val cluster = DataConnection.cluster
  override implicit lazy val session: Session = DataConnection.session
}

object DataConnection {
  val config = ConfigFactory.load()
  val hosts: Seq[String] = Config.config.getStringList("cassandra.host").toList
  val inets = hosts.map(InetAddress.getByName)
  val keySpace: KeySpace = KeySpace(Config.config.getString("cassandra.keyspace"))

  val cluster =
    Cluster.builder()
      .addContactPoints(inets)
      .withClusterName(Config.config.getString("cassandra.cluster"))
//      .withCredentials(config.getString("cassandra.username"), config.getString("cassandra.password"))
      .build()
  val session: Session = cluster.connect(keySpace.name)
} 
开发者ID:Thulebona,项目名称:hashpvtapi,代码行数:50,代码来源:Connection.scala

示例2: TFLMongoDBConnection

//设置package包名称以及导入依赖的类
package database.tfl

import com.mongodb.casbah.commons.MongoDBObject
import com.mongodb.casbah.{MongoCollection, MongoClient}
import database.{DatabaseCollections, PREDICTION_DATABASE}
import play.api.Logger

object TFLMongoDBConnection {
  lazy val mc: MongoClient = MongoClient()

  lazy val getDatabase = mc(PREDICTION_DATABASE.name)

  def getCollection(dbc:DatabaseCollections): MongoCollection = {
    val coll = getDatabase(dbc.name)
    createIndex(coll, dbc)
    Logger.info("Index Info: " + coll.getIndexInfo)
    coll
  }

  def closeConnection() = mc.close()

  def createIndex(mongoCollection: MongoCollection, dbc: DatabaseCollections) = {
    if (dbc.uniqueIndex) mongoCollection.createIndex(MongoDBObject(dbc.indexKeyList),MongoDBObject("unique" -> true))
    else mongoCollection.createIndex(MongoDBObject(dbc.indexKeyList),MongoDBObject("unique" -> false))
  }

} 
开发者ID:chrischivers,项目名称:London-Bus-Tracker-Play-Framework,代码行数:28,代码来源:TFLMongoDBConnection.scala

示例3: MongoDBConnection

//设置package包名称以及导入依赖的类
package cn.com.warlock.practice.sql

import com.mongodb.casbah.MongoClient
import com.mongodb.casbah.commons.MongoDBObject
import org.apache.log4j.Logger

class MongoDBConnection(host: String, port: Int, db: String, collection: String) {
  val logger = Logger.getLogger(getClass.getName)

  def this() = {
    this(Params.mongodbHost, Params.mongodbPort, Params.mongodbDB, Params.mongodbCollection)
  }

  val mongoClient = MongoClient(host, port)

  val dbConn = mongoClient(db)
  val collConn = dbConn(collection)
}

object MongoDBConnection {
  @volatile private var instance: MongoDBConnection = null

  def getInstance: MongoDBConnection = {
    if (instance == null) {
      synchronized {
        if (instance == null) {
          instance = new MongoDBConnection
        }
      }
    }

    instance
  }
} 
开发者ID:warlock-china,项目名称:spark-meepo,代码行数:35,代码来源:MongoDBConnection.scala


注:本文中的com.mongodb.casbah.MongoClient类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。