scala-presto is a Scala client for the Presto SQL engine that I built for fun, so be careful if you want to use this in production :) The client exposes three easy to use interfaces: a cursor interface, a callback interface, and an actor interface.
client.submitQuery(query).foreach {
queryResult => println(queryResult)
}
client.submitQuery(query).filter {
result => result.getData != null
}.foreach {
result => result.getData.asScala.foreach {
println
}
}
val data = for {
queryResult <- client.submitQuery(query) if queryResult.getData != null
} yield queryResult.getData.asScala
val nRows = data.foldLeft(0)((c,list) => c + list.size)
println(s"${nRows} rows retrieved")
client.submitQuery(query,
queryResult => {
println(s"got result ${queryResult}")
})
class ClientActor (prestoActor: ActorRef) extends Actor {
def receive = {
case QUERY_RESULTS (queryResults) => {
if(queryResults.getData != null) {
queryResults.getData.asScala.map(data => {
println(data)
})
}
if (queryResults.getNextUri == null) { //no more results
println("Shutting down actor system")
context.system.shutdown()
}
}
case SUBMIT_QUERY (query) => {
prestoActor ! SUBMIT_QUERY(query)
}
case _ => println("Unknown message")
}
}
object ActorSample extends BaseSample{
def main(args: Array[String]) {
val client = createPrestoClient(args)
val query = args(0)
val schema = args(1)
val catalog = args(2)
println(s"Will run query=${query} on schema=${schema} catalog=${catalog}")
val system = ActorSystem("PrestoTestActorSystem")
val prestoActor = system.actorOf(Props(new PrestoActor(client.config)), name = "presto_actor")
val prestoClientActor = system.actorOf(Props(new ClientActor(prestoActor)), name = "client_actor")
prestoClientActor ! SUBMIT_QUERY(query)
}
}
val cursor = client.submitQuery(query)
cursor.foreach {
queryResult => println(queryResult)
}
println("Query statistics:")
client.getQueryStatistics(queryId, statistics =>
for ((key,value) <- statistics)
println(s"${key} => ${value}")
)
client.forEachPrestoNode(prestoNode => println(prestoNode))
Add the following dependency to your build script
"com.github.nezihyigitbasi" %% "presto-scala-client" % "0.2"
sbt doc package