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


Scala MongoClient类代码示例

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


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

示例1: MongoDBSink

//设置package包名称以及导入依赖的类
package com.jetprobe.fastgen.io

import org.mongodb.scala.bson.BsonDocument
import org.mongodb.scala.{Completed, MongoClient, MongoCollection, Observer}
import org.mongodb.scala.bson.collection.mutable.Document
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}


case class MongoDBSink(db: String, collection: String, host: String)
    extends DataSink {

  import MongoDBSink._

  override def write(data: Array[String]): Unit = {
    val collection = getCollection(this)

    data
      .grouped(1000)
      .foreach(docs => {
        val observable =
          collection.insertMany(docs.map(str => Document(BsonDocument(str))))
        Await.result(observable.head(), 10 seconds)
      })
    logger.info(s"Total docs inserted : ${data.length}")
  }

}

object MongoDBSink {

  import org.json4s._
  import org.json4s.jackson.JsonMethods._

  def apply(uri: String): MongoDBSink = {
    val splitUri = uri.substring(10).split("/")
    val hostname = "mongodb://" + splitUri(0)
    val database = splitUri(1)
    val collection = splitUri(2).split("\\?")(0)
    MongoDBSink(database, collection, hostname)
  }

  def getCollection(mongo: MongoDBSink): MongoCollection[Document] = {
    val mongoClient = MongoClient(mongo.host)
    mongoClient.getDatabase(mongo.db).getCollection(mongo.collection)
  }

  def jsonStrToMap(jsonStr: String): Map[String, Any] = {
    implicit val formats = org.json4s.DefaultFormats
    parse(jsonStr).extract[Map[String, Any]]
  }
} 
开发者ID:amezng,项目名称:fastgen,代码行数:53,代码来源:MongoDBSink.scala

示例2: Examples

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

import org.mongodb.scala.MongoClient
import org.scalatest.{Matchers, WordSpec}

import scala.concurrent.Await
import scala.concurrent.duration._

class Examples extends WordSpec with Matchers with MongoScalaTest {

  lazy val mongoClient = MongoClient(s"mongodb://$embedConnectionURL:$embedConnectionPort")

  "MongoUnit" should {
    "load single json from resources" in {
      LoadFromResource("./testdata/singleJson.json") ~> {
        val result = mongoClient.getDatabase(defaultDatabase).getCollection(defaultCollection).find().toFuture()
        val report = Await.result(result, 10.second)
        report should have size 1
        report.head.get("id").foreach(x => x.asString().getValue shouldEqual "111-111-111-111")
      }
    }

    "load several files" in {
      LoadFromResource("./testdata/singleJson.json") ~>
        LoadFromResource("./testdata/singleJson.json") ~>
        LoadFromResource("./testdata/singleJson.json") ~> {
        val result = mongoClient.getDatabase(defaultDatabase).getCollection(defaultCollection).find().toFuture()
        val report = Await.result(result, 10.second)
        report should have size 3
        report.head.get("id").foreach(x => x.asString().getValue shouldEqual "111-111-111-111")
      }
    }
  }

} 
开发者ID:mielientiev,项目名称:MongoUnit,代码行数:36,代码来源:Examples.scala

示例3: DatabaseActor

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

import akka.actor.{Actor, ActorLogging}
import com.edu.chat.Settings
import com.mongodb.ServerAddress
import org.mongodb.scala.bson.collection.immutable.Document
import org.mongodb.scala.connection.ClusterSettings
import org.mongodb.scala.{MongoClient, MongoClientSettings, MongoCollection, MongoCredential, MongoDatabase}

import scala.collection.JavaConverters._

abstract class DatabaseActor extends Actor with ActorLogging {
  private val applicationSettings = Settings(context.system)

  protected val collectionName: String

  protected def loadCollection: MongoCollection[Document] = {
    val clusterSettings = ClusterSettings
      .builder()
      .hosts(List(new ServerAddress(applicationSettings.dbHost, applicationSettings.dbPort)).asJava)
      .build()

    val credentials = List(MongoCredential.createCredential(
      applicationSettings.dbUsername,
      applicationSettings.dbName,
      applicationSettings.dbPassword)).asJava

    val dbSettings = MongoClientSettings
      .builder()
      .clusterSettings(clusterSettings)
      .credentialList(credentials)
      .build()

    val mongoDbClient = MongoClient(dbSettings)

    val database: MongoDatabase = mongoDbClient.getDatabase(applicationSettings.dbName)
    database.getCollection(collectionName)
  }
} 
开发者ID:themirrortruth,项目名称:chat-akka,代码行数:40,代码来源:DatabaseActor.scala

示例4: Example3

//设置package包名称以及导入依赖的类
package com.stulsoft.pmongodb.example01

import com.stulsoft.pmongodb.example01.Helpers._
import com.typesafe.scalalogging.LazyLogging
import org.bson.codecs.configuration.CodecRegistries.{fromProviders, fromRegistries}
import org.mongodb.scala.bson.ObjectId
import org.mongodb.scala.bson.codecs.DEFAULT_CODEC_REGISTRY
import org.mongodb.scala.bson.codecs.Macros._
import org.mongodb.scala.model.Filters._
import org.mongodb.scala.{MongoClient, MongoCollection, MongoDatabase}


object Example3 extends App with LazyLogging {

  private lazy val codecRegistry = fromRegistries(fromProviders(classOf[Person]), DEFAULT_CODEC_REGISTRY)

  // To directly connect to the default server localhost on port 27017
  val client: MongoClient = MongoClient()
  val database: MongoDatabase = client.getDatabase("testDb").withCodecRegistry(codecRegistry)
  val collection: MongoCollection[Person] = database.getCollection("testCollection3")
  collection.drop().results()

  val person: Person = Person("Ada", "Lovelace")
  collection.insertOne(person).results()

  collection.find().first().printHeadResult()
  collection.find().printResults()
  collection.find(equal("firstName", "Ada")).first().printHeadResult()
  collection.find(equal("lastName", "Lovelace")).first().printHeadResult()

  case class Person(_id: ObjectId, firstName: String, lastName: String)

  object Person {
    def apply(firstName: String, lastName: String): Person =
      Person(new ObjectId(), firstName, lastName)
  }

} 
开发者ID:ysden123,项目名称:poc,代码行数:39,代码来源:Example3.scala

示例5: Example1

//设置package包名称以及导入依赖的类
package com.stulsoft.pmongodb.example01

import com.stulsoft.pmongodb.example01.Helpers._
import com.typesafe.scalalogging.LazyLogging
import org.mongodb.scala.MongoClient
import org.mongodb.scala.bson.collection.immutable.Document


object Example1 extends App with LazyLogging {
  logger.info("start")
  try {
    // To directly connect to the default server localhost on port 27017
    val dbClient: MongoClient = MongoClient()
    val database = dbClient.getDatabase("testDb")
    val collection = database.getCollection("testCollection")
    val testDoc = Document("name" -> "row1")

    val results = collection.insertOne(testDoc).results()

    logger.debug(s"results: $results")

    val count = collection.count().results().head
    logger.debug(s"count is $count")
    collection.drop().headResult()
  }
  catch {
    case e: Exception => println(s"Error: ${e.getMessage}")
  }
  logger.info("finish")
} 
开发者ID:ysden123,项目名称:poc,代码行数:31,代码来源:Example1.scala

示例6: Example2

//设置package包名称以及导入依赖的类
package com.stulsoft.pmongodb.example01

import com.stulsoft.pmongodb.example01.Helpers._
import com.typesafe.scalalogging.LazyLogging
import org.mongodb.scala.MongoClient
import org.mongodb.scala.bson.collection.immutable.Document


object Example2 extends App with LazyLogging {

  case class MyDocument(name: String, age: Int) {
    def toDocument: Document = {
      Document("name" -> name, "age" -> age)
    }
  }

  logger.info("start")
  try {
    val dbClient: MongoClient = MongoClient()
    val database = dbClient.getDatabase("testDb")
    val collection = database.getCollection("testCollection2")
    val testDoc = MyDocument("the name", 21).toDocument

    val results = collection.insertOne(testDoc).results()

    logger.debug(s"results: $results")

    val count = collection.count().results().head
    logger.debug(s"count is $count")

    collection.find().printResults()
    collection.drop().headResult()
  }
  catch {
    case e: Exception => println(s"Error: ${e.getMessage}")
  }
  logger.info("finish")

} 
开发者ID:ysden123,项目名称:poc,代码行数:40,代码来源:Example2.scala

示例7: MongoService

//设置package包名称以及导入依赖的类
package service

import java.util.concurrent.TimeUnit.SECONDS

import org.bson.json.JsonMode
import org.bson.json.JsonWriterSettings
import org.mongodb.scala.MongoClient
import org.mongodb.scala.MongoCollection
import org.mongodb.scala.MongoDatabase
import org.mongodb.scala.bson.collection.immutable.Document

import scala.concurrent.Await
import scala.concurrent.Future
import scala.concurrent.duration.Duration
import scala.language.postfixOps
import scala.util.Failure
import scala.util.Success
import scala.util.Try

class MongoService(mongoQueryParser: MongoQueryParser) {

  // You will only need one instance of class MongoClient even with multiple concurrently executing asynchronous operations.
  val mongoClient: MongoClient = MongoClient("mongodb://localhost")
  val database: MongoDatabase = mongoClient.getDatabase("vm")
  val collection: MongoCollection[Document] = database.getCollection("letters")

  def executeQuery(query: String): Seq[String] = Try {
    val result: Future[Document] = collection.find(mongoQueryParser.parse(query)).first().head()
    List(Await.result(result, Duration(30, SECONDS)).toJson(new JsonWriterSettings(JsonMode.STRICT, true)))
  } match {
    case Success(value) => value
    case Failure(f) => List()
  }
} 
开发者ID:dougsleite,项目名称:oak,代码行数:35,代码来源:MongoService.scala

示例8: Database

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

import fresco.db.Helpers._
import org.mongodb.scala.bson.collection.immutable.Document
import org.mongodb.scala.{MongoClient, MongoCollection}


object Database {
  val mongoClient = MongoClient()
  val mongoDb = mongoClient.getDatabase("fresco")

  implicit def printResults() = {

  }

  def getStores() = {
    val collection: MongoCollection[Document] = mongoDb.getCollection("stores")
    val doc: Document = Document("_id" -> 0, "name" -> "MongoDB", "type" -> "database",
      "count" -> 1, "info" -> Document("x" -> 203, "y" -> 102))
    // collection.insertOne(doc).results()
    collection.find().printResults()
  }

}

object TestDatabase extends App {
  val stores = Database.getStores()
  println(s"$stores")
} 
开发者ID:ksuhail7,项目名称:eCabinet,代码行数:30,代码来源:Database.scala

示例9: Main

//设置package包名称以及导入依赖的类
package de.khamrakulov.main.shardedCluster

import akka.NotUsed
import akka.actor.ActorSystem
import akka.stream.{ActorMaterializerSettings, Supervision, ActorMaterializer}
import akka.stream.scaladsl.{Merge, Source}
import com.mongodb.MongoException
import com.typesafe.config.ConfigFactory
import de.khamrakulov.configs.MongoConfig
import de.khamrakulov.models
import de.khamrakulov.services.{OplogService, ShardService}
import org.mongodb.scala.MongoClient
import org.mongodb.scala.bson.Document


object Main extends App {

  val config = ConfigFactory.load()

  val mongoConfig = MongoConfig(config.getConfig("mongodb-sharded-cluster"))

  val mongo = MongoClient(mongoConfig.uri)
  val shardService = ShardService(mongo)
  val oplog = OplogService()

  implicit val system = ActorSystem("MongoOplogTailer")

  implicit val materializer = ActorMaterializer()

  val shardOplogSources = shardService.getShards.map({ shard =>
    val client = MongoClient(shard.uri)
    oplog.source(client)
  })

  val allShards: Source[Document, NotUsed] = shardOplogSources.foldLeft(Source.empty[Document]) {
    (prev, current) => Source.combine(prev, current)(Merge(_))
  }

  allShards.map(models.documentToOplogEntry).runForeach(println)

} 
开发者ID:htimur,项目名称:mongo-oplog-akka-streams,代码行数:42,代码来源:Main.scala

示例10: getShards

//设置package包名称以及导入依赖的类
package de.khamrakulov.services

import de.khamrakulov.configs.MongoConstants
import de.khamrakulov.models.Shard
import org.mongodb.scala.{Document, MongoClient}

import scala.concurrent.Await
import scala.concurrent.duration._


trait ShardService {
  def getShards: Seq[Shard]
}

object ShardService {

  def apply(client: MongoClient) = new ShardServiceImpl(client)

  final class ShardServiceImpl(client: MongoClient) extends ShardService {

    private val collection = client.getDatabase(MongoConstants.CONFIG_DATABASE)
      .getCollection(MongoConstants.SHARDS_COLLECTION)

    override def getShards = {
      val shards = collection.find().map(parseShardInformation).toFuture()
      Await.result(shards, 10.seconds)
    }

    private def parseShardInformation(item: Document): Shard = {
      val document = item.toBsonDocument
      val shardId = document.getString(MongoConstants.SHARDS_ID).getValue
      val serversDefinition = document.getString(MongoConstants.SHARDS_HOST).getValue
      val servers = if (serversDefinition.contains("/")) serversDefinition.substring(serversDefinition.indexOf('/') + 1) else serversDefinition
      Shard(shardId, "mongodb://" + servers)
    }
  }

} 
开发者ID:htimur,项目名称:mongo-oplog-akka-streams,代码行数:39,代码来源:ShardService.scala

示例11: source

//设置package包名称以及导入依赖的类
package de.khamrakulov.services

import akka.NotUsed
import akka.stream.scaladsl.Source
import com.mongodb.CursorType
import de.khamrakulov.configs.MongoConstants
import org.mongodb.scala.bson.collection.immutable.Document
import org.mongodb.scala.model.Filters._
import org.mongodb.scala.{FindObservable, MongoClient}

trait OplogService {

  def source(client: MongoClient): Source[Document, NotUsed]

}

object OplogService {
  def apply() = new OplogServiceImpl

  class OplogServiceImpl extends OplogService {

    import rxStreams.Implicits._

    override def source(client: MongoClient): Source[Document, NotUsed] = {
      val observable = getOplogObservable(client)

      Source.fromPublisher(observable)
    }


    private def getOplogObservable(client: MongoClient): FindObservable[Document] = {
      client.getDatabase(MongoConstants.LOCAL_DATABASE)
        .getCollection(MongoConstants.OPLOG_COLLECTION)
        .find(and(
          in(MongoConstants.OPLOG_OPERATION, "i", "d", "u"),
          exists(MongoConstants.OPLOG_FROM_MIGRATE, exists = false)))
        .cursorType(CursorType.TailableAwait)
        .noCursorTimeout(true)
    }
  }

} 
开发者ID:htimur,项目名称:mongo-oplog-akka-streams,代码行数:43,代码来源:OplogService.scala

示例12: Boot

//设置package包名称以及导入依赖的类
package com.github.cupenya.auth.service

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import com.github.cupenya.auth.service.health.{HealthCheck, HealthCheckRoute, HealthCheckService, MongoDbHealthCheck}
import org.mongodb.scala.MongoClient

import scala.concurrent.Future

object Boot extends App with Logging with AuthHttpService with UserAuthService with HealthCheckRoute with HealthCheckService {
  implicit val system = ActorSystem()
  implicit val ec = system.dispatcher
  implicit val materializer = ActorMaterializer()

  log.info(s"Starting Auth service")

  private val dbUri = Config.databases.default.uri
  private val dbName = Config.databases.default.db

  log.info(s"Connecting to $dbUri/$dbName")

  val mongoClient = MongoClient(dbUri)
  implicit val db = mongoClient.getDatabase(dbName)

  Http().bindAndHandle(healthRoute ~ authRoute, Config.http.interface, Config.http.port).transform(
    binding => log.info(s"REST interface bound to ${binding.localAddress} "),
    { t => log.error(s"Couldn't start Auth service", t); sys.exit(1) }
  )

  override def checks: List[HealthCheck] = List(new MongoDbHealthCheck(mongoClient))
} 
开发者ID:cupenya,项目名称:auth-service,代码行数:33,代码来源:Boot.scala

示例13: DatabaseContext

//设置package包名称以及导入依赖的类
package ai.snips.bsonmacros

import javax.inject._

import org.mongodb.scala.{MongoClient, MongoDatabase}
import play.api._
import play.api.inject.ApplicationLifecycle

import scala.concurrent._

@Singleton
class DatabaseContext @Inject()(val conf: Configuration,
                                applicationLifecycle: ApplicationLifecycle)
                               (implicit val ec: ExecutionContext) {

  lazy val mongoConf: String = conf.getOptional[String]("mongodb.uri").getOrElse("mongodb://localhost")
  lazy val client = MongoClient(mongoConf)
  lazy val codecRegistry = new DynamicCodecRegistry

  applicationLifecycle.addStopHook { () =>
    Future.successful(client.close())
  }

  def database(name: String): MongoDatabase =
    client.getDatabase(name).withCodecRegistry(codecRegistry)

  def ping(): Future[Unit] =
    client.listDatabaseNames().toFuture.map(_ => ())
} 
开发者ID:snipsco,项目名称:play-mongo-bson,代码行数:30,代码来源:DatabaseContext.scala

示例14: dataStore

//设置package包名称以及导入依赖的类
package g8.akkahttp.eventsvc.data.access

import g8.akkahttp.eventsvc.Config
import g8.akkahttp.eventsvc.data.Event
import org.mongodb.scala.bson.BsonDocument
import org.mongodb.scala.model.Filters
import org.mongodb.scala.{Completed, MongoClient, MongoCollection, Observer}

import scala.concurrent.{Future, Promise}

trait MongoDataStoreImpl extends DataStoreComponent  {

  private val _dataStoreImpl = new DataStoreImpl
  def dataStore: DataStore = _dataStoreImpl

  class DataStoreImpl extends DataStore {

    // EVENTS

    override def createEvents(numToCreate: Int): Future[Boolean] = {
      val p = Promise[Boolean]()
      val database = MongoDataStoreConnection.client.getDatabase("sdsi").withCodecRegistry(Event.codecRegistry)
      val collection: MongoCollection[Event] = database.getCollection("events")

      val events = for (i <- 0 until numToCreate) yield Event.generateRandom

      collection.insertMany(events).subscribe(new Observer[Completed] {
        override def onNext(res: Completed): Unit = {}
        override def onError(e: Throwable): Unit = { println(s"error occurred ${e.getMessage}"); p.failure(e) }
        override def onComplete(): Unit = { println("completed"); p.success(true) }
      })

      p.future
    }

    override def getEventsByType(triggerType: Int): Future[List[BsonDocument]] = {
      val p = Promise[List[BsonDocument]]
      val database = MongoDataStoreConnection.client.getDatabase("sdsi")
      val collection: MongoCollection[BsonDocument] = database.getCollection("events")

      collection.find(Filters.`eq`("trigger.triggerType", triggerType)).collect().subscribe(
        (results: Seq[BsonDocument]) => p.success(results.toList),
        (error: Throwable) => p.failure(error)
      )

      p.future
    }
  }
}

object MongoDataStoreConnection extends Config {

  private val uri: String = config.getString("mongoConnectionUri")
  System.setProperty("org.mongodb.async.type", "netty")

  // TODO: Introduce the standard UUID codec for compatibility with other drivers

  lazy val client: MongoClient = MongoClient(uri)
} 
开发者ID:stephmajor,项目名称:eventsvc,代码行数:60,代码来源:MongoDataStoreImpl.scala

示例15: ReminderRepository

//设置package包名称以及导入依赖的类
package de.neuland.repositories

import com.typesafe.config.{Config, ConfigFactory}
import de.neuland.models.ReminderRecord
import org.bson.codecs.configuration.CodecRegistries.{fromProviders, fromRegistries}
import org.bson.codecs.configuration.CodecRegistry
import org.mongodb.scala.bson.codecs.DEFAULT_CODEC_REGISTRY
import org.mongodb.scala.bson.codecs.Macros._
import org.mongodb.scala.model.Filters._
import org.mongodb.scala.{Completed, MongoClient, MongoCollection, MongoDatabase}
import play.api.Logger

import scala.concurrent.Future

class ReminderRepository {
  val conf: Config = ConfigFactory.load

  val codecRegistry: CodecRegistry = fromRegistries(fromProviders(classOf[ReminderRecord]), DEFAULT_CODEC_REGISTRY )
  val mongoClient: MongoClient = MongoClient(conf.getString("db.mongo"))
  val database: MongoDatabase = mongoClient.getDatabase(conf.getString("db.database")).withCodecRegistry(codecRegistry)
  val collection: MongoCollection[ReminderRecord] = database.getCollection("reminders")

  def save(reminderRecord: ReminderRecord): Future[Completed] = {
    Logger.debug("Save " + reminderRecord)
    collection.insertOne(reminderRecord).head()
  }

  def all(): Future[Seq[ReminderRecord]] =  {
    collection.find().toFuture()
  }

  def find(author: String): Future[Seq[ReminderRecord]] = {
    collection.find(equal("author", author)).toFuture()
  }

} 
开发者ID:neuland,项目名称:matterminder,代码行数:37,代码来源:ReminderRepository.scala


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