scala - How to extract the value of a cell from Future[ResultSet]? -
i data in format, example:
id -- 23
to future[resultset].
the code serve same purpose without using async shown below.
{ 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 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 }
how perform same action with:
val results: future[resultset] = session.executeasync(query)
you can extract result using oncomplete
callback of 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() }
or can wait until executed
val fut = result.map {r => r.one().getint(0)} val id = await.result(fut, 10 seconds) id
Comments
Post a Comment