Skip to content

Instantly share code, notes, and snippets.

@jairamc
Created March 30, 2017 13:21
Show Gist options
  • Save jairamc/d655becd40eca9166f9806f6fbb4c036 to your computer and use it in GitHub Desktop.
Save jairamc/d655becd40eca9166f9806f6fbb4c036 to your computer and use it in GitHub Desktop.
KamonJdbcExample
import SampleDatabase._
import slick.jdbc.H2Profile.api._
import kamon.Kamon
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.concurrent.duration._
import java.util.concurrent.{Executors, TimeUnit}
object SampleDatabase {
// Definition of the SUPPLIERS table
class Suppliers(tag: Tag) extends Table[(Int, String, String, String, String, String)](tag, "SUPPLIERS") {
def id = column[Int]("SUP_ID", O.PrimaryKey) // This is the primary key column
def name = column[String]("SUP_NAME")
def street = column[String]("STREET")
def city = column[String]("CITY")
def state = column[String]("STATE")
def zip = column[String]("ZIP")
// Every table needs a * projection with the same type as the table's type parameter
def * = (id, name, street, city, state, zip)
}
val suppliers = TableQuery[Suppliers]
// Definition of the COFFEES table
class Coffees(tag: Tag) extends Table[(String, Int, Double, Int, Int)](tag, "COFFEES") {
def name = column[String]("COF_NAME", O.PrimaryKey)
def supID = column[Int]("SUP_ID")
def price = column[Double]("PRICE")
def sales = column[Int]("SALES")
def total = column[Int]("TOTAL")
def * = (name, supID, price, sales, total)
// A reified foreign key relation that can be navigated to create a join
def supplier = foreignKey("SUP_FK", supID, suppliers)(_.id)
}
val coffees = TableQuery[Coffees]
}
object KamonJdbcExample extends App {
Kamon.start()
val executorService = Executors.newScheduledThreadPool(4)
implicit val executionContext = ExecutionContext.fromExecutorService(executorService)
val db = Database.forConfig("h2mem1")
println("Setting up DB...")
Await.ready(db.run(DBIO.seq(
// Create the tables, including primary and foreign keys
(suppliers.schema ++ coffees.schema).create,
// Insert some suppliers
suppliers += (101, "Acme, Inc.", "99 Market Street", "Groundsville", "CA", "95199"),
suppliers += ( 49, "Superior Coffee", "1 Party Place", "Mendocino", "CA", "95460"),
suppliers += (150, "The High Ground", "100 Coffee Lane", "Meadows", "CA", "93966"),
// Equivalent SQL code:
// insert into SUPPLIERS(SUP_ID, SUP_NAME, STREET, CITY, STATE, ZIP) values (?,?,?,?,?,?)
// Insert some coffees (using JDBC's batch insert feature, if supported by the DB)
coffees ++= Seq(
("Colombian", 101, 7.99, 0, 0),
("French_Roast", 49, 8.99, 0, 0),
("Espresso", 150, 9.99, 0, 0),
("Colombian_Decaf", 101, 8.99, 0, 0),
("French_Roast_Decaf", 49, 9.99, 0, 0)
)
// Equivalent SQL code:
// insert into COFFEES(COF_NAME, SUP_ID, PRICE, SALES, TOTAL) values (?,?,?,?,?)
)), 5 seconds)
println("DB setup complete.")
def q1 = db.run(coffees.result).map(_.foreach {
case (name, supID, price, sales, total) =>
println(" " + name + "\t" + supID + "\t" + price + "\t" + sales + "\t" + total)
})
def q2 = db.stream(
(for (c <- coffees)
yield LiteralColumn(" ") ++ c.name ++ "\t" ++ c.supID.asColumnOf[String] ++
"\t" ++ c.price.asColumnOf[String] ++ "\t" ++ c.sales.asColumnOf[String] ++
"\t" ++ c.total.asColumnOf[String]).result
).foreach(println)
def q3 = db.stream(
(for {
c <- coffees if c.price < 9.0
s <- suppliers if s.id === c.supID
} yield (c.name, s.name)).result
).foreach(println)
def q4 = db.stream(
(for {
c <- coffees if c.price < 9.0
s <- c.supplier
} yield (c.name, s.name)).result
).foreach(println)
val runner = new Runnable {
override def run(): Unit = {
Await.ready(Future.sequence(List(q1, q2, q3, q4)), 10 seconds)
}
}
executorService.scheduleWithFixedDelay(runner, 0, 2, TimeUnit.SECONDS)
sys.addShutdownHook {
executorService.shutdown()
db.close()
Kamon.shutdown()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment