Created
February 23, 2016 19:55
-
-
Save developmentalmadness/341e2aa0dfb7c8961c9c to your computer and use it in GitHub Desktop.
Query postgresql database using Slick streaming
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
pg-postgres = { | |
url = ${DB_PG_URL}/postgres | |
user = ${DB_PG_USER} | |
password = ${DB_PG_PWD} | |
driver = org.postgresql.Driver | |
} |
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
name := """slick-postgres-query""" | |
version := "1.0" | |
scalaVersion := "2.11.7" | |
val akkaVersion = "2.4.2" | |
libraryDependencies ++= List( | |
"com.typesafe.akka" %% "akka-slf4j" % akkaVersion, | |
"com.typesafe.slick" %% "slick" % "3.0.0", | |
"org.postgresql" % "postgresql" % "9.4-1201-jdbc41", | |
"ch.qos.logback" % "logback-classic" % "1.1.3", | |
"com.zaxxer" % "HikariCP" % "2.4.1", | |
"com.typesafe.slick" %% "slick-codegen" % "3.0.2" % "provided", | |
"com.typesafe.akka" %% "akka-actor" % akkaVersion, | |
"com.typesafe.akka" %% "akka-stream" % akkaVersion | |
) |
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
package com.dvMENTALmadness | |
import akka.actor.ActorSystem | |
import slick.driver.PostgresDriver.api._ | |
import scala.collection.immutable.Stream._ | |
import scala.concurrent.duration._ | |
import scala.concurrent.{Future, Await} | |
import akka.stream._ | |
import akka.stream.scaladsl._ | |
object RunQuery { | |
def main(args: Array[String]) : Unit = { | |
implicit val system = ActorSystem("RunQuery") | |
implicit val materializer = ActorMaterializer() | |
val strm = TableQuery[Records] | |
val db = Database.forConfig("pg-postgres") | |
try{ | |
val src = Source.fromPublisher(db.stream(strm.result)) | |
Await.ready(src.runForeach(r => println(s"${r.id},${r.value}")), Duration.Inf) | |
} finally { | |
system.shutdown | |
db.close | |
} | |
} | |
} | |
case class Record(id: Int, value: String) | |
class Records(tag: Tag) extends Table[Record](tag, "my_stream") { | |
def id = column[Int]("id") | |
def value = column[String]("value") | |
def * = (id, value) <> (Record.tupled, Record.unapply) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment