本文整理汇总了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)
}
示例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))
}
}
示例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
}
}