Reputation: 53
How I would build a scala query to return
one row of my table at a time?
My tables are in the following location if they help in answering this question: Slick 3.0 (scala) queries don't return data till they are run multiple times (I think)
val q5 = for {
c <- dat.patientsss
} yield (c.PID, c.Gender, c.Age, c.Ethnicity)
Await.result((db.stream(q5.result).foreach(println)),Duration.Inf)
but instead of printing, I need return each.
Upvotes: 0
Views: 1328
Reputation: 5699
Use a materialized result instead:
val result = Await.result((db.run(q5.result)), Duration.Inf)
result
is a Seq
that contains all your patient data. Use foreach
to iterate over the result set:
result.foreach(r => yourFancyAlgorithm(r)) // r is a single patients data row
Await
blocks the current thread making one of slick's best features obsolete. Blocking threads is something you should not do. I highly recommend to read about Future
and Promise
in scala.
The above example can be simply written as:
val result = db.run(q5.result))
result
in this case will be of type Future[Seq[(yourPatientsData)]]
. To access the data, use map
on the result:
result.map(d => whatever) // d is of type Seq[(yourPatientsData)]
While waiting for the result, the rest of your application will continue to do its calculations and stuff. Finally when the result is ready, the callback (d => whatever
) will run.
Upvotes: 2