Git Product home page Git Product logo

presto-scala-client's Introduction

##scala-presto

scala-presto is a Scala client for the Presto SQL engine. The client exposes three easy to use interfaces: a cursor interface, a callback interface, and an actor interface.

##Usage

###Cursor Usage

####Print the raw query results

client.submitQuery(query).foreach {
  queryResult => println(queryResult)
}

####Print each list of data in the results

client.submitQuery(query).filter {
  result => result.getData != null
}.foreach {
  result => result.getData.asScala.foreach {
    println
  }
}

####Can also use for-comprehensions

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")

###Callback Usage

client.submitQuery(query,
  queryResult => {
    println(s"got result ${queryResult}")
  })

###Actor Usage

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)
  }
}

###Other Functionality

####Query Statistics

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}")
)

####Getting All Nodes

client.forEachPrestoNode(prestoNode => println(prestoNode))

##Using the Library Add the following dependency to your build script

"com.github.nezihyigitbasi" %% "presto-scala-client" % "0.2"

##Building from the Source

sbt doc package

presto-scala-client's People

Contributors

nezihyigitbasi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.