Skip to content

Instantly share code, notes, and snippets.

@jbrisbin
Last active January 27, 2016 16:46
Show Gist options
  • Save jbrisbin/7e491ba74157f5ee5f9b to your computer and use it in GitHub Desktop.
Save jbrisbin/7e491ba74157f5ee5f9b to your computer and use it in GitHub Desktop.
// case classes should be used instead of builders.
// Query is a case class that has all the appropriate parameters on it.
val query = Query("select * from ...")
case class LondonAirQuality(family:String, series:String, measurementDate:Long)
// Use standard Scala Try match/case semantics for execute
val jsonResult = Try(client.execute(query)) {
case Success(result) => {
// Result is natively iterable and can be mapped to another collection using Scala collections APIs
result.map { row =>
// Each row can be mapped to a strongly-typed case class that unwraps the columns into types automatically
row match {
case LondonAirQuality(londonAir) => {
JSON.write(Map("measurementDate" -> londonAir.measurementDate))
}
case _ => throw new IllegalStateException("Unknown row type $row")
}
}
}
case err => "default fallback value"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment