Last active
January 27, 2016 16:46
-
-
Save jbrisbin/7e491ba74157f5ee5f9b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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