我在Scala 2.11.8上使用Slick 3.1.0。这是使用光滑的扩展来查询Oracle数据库。
我写了以下代码
type MyTup = (String, String, String)
implicit val actorSystem = ActorSystem()
implicit val materializer = ActorMaterializer()
val db = Database.forConfig("oracle")
val query = sql"""select col1, col2, col3 from FOO.BAR""".as[MyTup]
val source = Source.fromPublisher[MyTup](db.stream(query))
val sink = Sink.foreach[MyTup](row => println(row))
val flow = source to sink
flow.run()
readLine()
db.close()
当我运行此代码时,此代码运行很长时间而不打印任何内容....这似乎意味着它试图在打印之前读取整个结果(因此它不是流式传输)。
我已经在MySQL上完成了流式传输,我知道它需要特殊配置来启用流式传输
https://github.com/slick/slick/issues/1218
oracle是否还需要一些特殊的配置,比如MySql?
我的build.sbt
libraryDependencies ++= Seq(
"com.typesafe.slick" % "slick-extensions_2.11" % "3.1.0",
"com.typesafe.slick" % "slick_2.11" % "3.1.0",
"com.typesafe.slick" % "slick-codegen_2.11" % "3.1.0",
"oracle" % "ojdbc7_2.11" % "7.0.0",
"com.typesafe.slick" % "slick-hikaricp_2.11" % "3.1.0",
"com.typesafe.akka" % "akka-stream_2.11" % "2.5.4"
)
答案 0 :(得分:0)
行。我找到了答案。是的Oracle驱动程序确实支持流式传输,它不需要像mysql那样的任何额外配置。
这是我的application.conf
oracle {
driver = "slick.jdbc.OracleProfile$"
dataSourceClass = "slick.jdbc.DatabaseUrlDataSource"
properties {
driver = "oracle.jdbc.OracleDriver"
url = "jdbc:oracle:thin:user/password@//server:1521/serviceName"
}
numThreads = 10
connectionTimeout = 10000
}
我的build.sbt
"com.typesafe.slick" %% "slick" % "3.2.1",
"com.typesafe.slick" %% "slick-hikaricp" % "3.2.1",
"oracle" % "ojdbc7_2.11" % "7.0.0",
"com.typesafe.akka" %% "akka-stream" % "2.5.4"
请注意,我没有使用光滑的扩展名,因为它们已经过时了。新的Oracle驱动程序(OracleProfile)位于光滑的核心版本中。
这里唯一的技巧是条目"oracle" % "ojdbc7_2.11" % "7.0.0"
是假的。因为我下载了jar并在本地sbt中发布了它。 (oracle不提供oracle驱动程序作为sbt / maven依赖)
现在这是我的代码,它可以非常精确地传输数据:)
implicit val actorSystem = ActorSystem()
implicit val actorMaterializer = ActorMaterializer()
val db = Database.forConfig("oracle")
val query = sql"select member_id from foo".as[Long]
val publisher = db.stream(query)
val source = Source.fromPublisher[Long](publisher)
val sink = Sink.foreach[Long](x => println(x))
val graph = RunnableGraph.fromGraph(GraphDSL.create(sink){implicit builder => s =>
import GraphDSL.Implicits._
source ~> s.in
ClosedShape
})
val future = graph.run()
Await.result(future, Duration.Inf)
db.close()