我以格式获取数据,例如:
id
--
23
到Future [ResultSet]。
不使用Async时用于相同目的的代码如下所示。
{
var last_seen_time: Long = 0
try {
val results: ResultSet = session.execute(query)
last_seen_time = results.one().getLong(0) * 1000
}
catch {
case e: Throwable => println("Error occurred in executing the query:" +
"Possible cause device not started sending data.")
}
println(s"current_time = $current_time")
println(s"message_post_interval = $message_post_interval")
println(s"last_seen_time = $last_seen_time")
if (current_time.toLong - message_post_interval.toLong <= last_seen_time)
true
else
false
}
如何使用以下方式执行相同的操作:
val results: Future[ResultSet] = session.executeAsync(query)
答案 0 :(得分:1)
您可以使用onComplete
Future
回调提取结果
val result : Future[ResultSet] = ...
result.map {r => r.one().getInt(0)} onComplete {
case Success(v) =>
println(s"Id = $v")
case Failure(e) =>
e.printStackTrace()
}
或者你可以等到它被执行
val fut = result.map {r => r.one().getInt(0)}
val id = Await.result(fut, 10 seconds)
id