【问题标题】:How can I retrieve data synchronously from Apache Cassandra using Phantom-dsl in Scala?如何在 Scala 中使用 Phantom-dsl 从 Apache Cassandra 同步检索数据?
【发布时间】:2018-11-12 19:04:27
【问题描述】:

我正在使用 Scala_2.11.7、Apache Cassasndra_3.9 和 Phantom-dsl_2.6.1。 当我使用 phantom 从 Cassandra 检索数据时,phantom 总是在 Future 中返回数据。但我想要同步的结果。有没有办法使用 phantom 从 cassandra 同步检索数据?

【问题讨论】:

  • 为什么不添加方法来包装对 Phantom 的调用,并在 Future 上使用 next .get
  • @AlexOtt 我不明白你在说什么。你能详细说明一下吗?
  • @jonyroy 你应该有一个非常强大的用例来同步做事,否则你会心甘情愿地接受性能瓶颈和反模式。 Phantom 竭尽全力保持所有执行异步,您应该利用这一点。

标签: scala cassandra phantom-dsl


【解决方案1】:

如果你希望你的执行是同步的,你必须阻塞调用线程并等待未来完成。这是(正如 scala lang doc 所说)“强烈反对”,因为性能严重受损。

如果你有一个返回Future[T]的函数,你可以使用Await.result,例如:

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

val fresult : Future[Int] = getRecord()

val result: Int = Await.result(fresult, 5 seconds) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 2016-04-10
    • 2015-09-16
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    相关资源
    最近更新 更多