简体   繁体   English

Scala Mongo驱动程序使用Future获得结果

[英]Scala Mongo driver getting results using Future

I am intending to fetch all records that match a criteria from Mongo using Scala Mongo Driver . 我打算使用Scala Mongo Driver从Mongo提取所有符合条件的记录。

Using Observables, you can access the stream by creating a subscription: 使用Observables,可以通过创建订阅来访问流:

val MaxBuffer: Long = 100
var docs: Queue[Document] = Queue.empty
var sub: Option[Subscription] = None

val q: Observable[Document]

def fetchMoreRecords: Unit = sub.get.request(MaxBuffer)

q.subscribe(new Observer[Document] {

  override def onSubscribe(subscription: Subscription): Unit = {
    sub = Some(subscription)
    fetchMoreRecords
  }

  override def onError(e: Throwable): Unit = fail(out, e)

  override def onComplete(): Unit = {
    println("Stream is complete")
    complete(out)
  }

  override def onNext(result: Document): Unit = {
    if (doc.size == maxBuffer) {
      fail(out, new RuntimeException("Buffer overflow"))
    } else {
      docs = docs :+ result
    }
  }

})

(this code is incomplete) (此代码不完整)

I would need a function like: 我需要一个类似的功能:

def isReady: Future[Boolean] = {}

Which completes whenever onNext was called at least once. 只要onNext至少被调用一次, onNext完成。 The bad way to do this would be: 这样做的坏方法是:

def isReady: Future[Boolean] = {
    Future {
        def wait: Unit = {
            if (docs.nonEmpty) {
                true
            } else { wait }
        }
        wait
    }
}

What would be the best way to achieve this? 实现这一目标的最佳方法是什么?

You want to use Promise : 您要使用Promise

 val promise = Promise[Boolean]()
 ...
 override def onNext() = {
   ...
   promise.tryComplete(Success(true))
}
override def onError(e: Throwable) = 
   promise.tryComplete(Failure(e))



val future = promise.future

You should do something to handle the case when there are no result (as it is now, the future will never be satisfied ... 如果没有结果,您应该采取一些措施来处理这种情况(因为现在,未来将永远无法满足...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 mongo-scala-driver 在 Scala 中打印来自 Mongodb 的查询结果 - Printing query results from Mongodb in Scala using mongo-scala-driver 使用 scala mongo 驱动程序序列化为对象? - Serialize to object using scala mongo driver? 案例 class 的继承元素未使用 scala mongo 驱动程序持久化到 mongodb - inherited elements of a case class not getting persisted into mongodb using scala mongo driver 使用反应式 Mongo 驱动程序时 Scala 控制台应用程序不会退出 - Scala Console App Does Not Exit When Using Reactive Mongo Driver 如何使用 scala 驱动程序将文档插入 Mongo 时间序列集合 - How to insert documents into a Mongo Time Series collection using scala driver 使用Scala的ReactiveMongo驱动程序将对象插入Mongo数据库 - Inserting an object into Mongo database using Scala's ReactiveMongo driver 如何使用Java驱动程序将mongo mapreduce结果存储在不同的数据库中 - how to store mongo mapreduce results in different database using java driver 使用java Driver从mongo db获取嵌套文档 - Getting nested Document from mongo db using java Driver 使用Mongo DB异步驱动程序将文档列表导入Java列表 - Getting list of Documents into Java List using Mongo DB Async Driver 如何使用 MongoDB Scala 驱动程序将聚合结果累积到集合中 - How to accumulate aggregation results into a collection using MongoDB Scala Driver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM