Scala - fromPublisher不是对象的成员akka.stream.scaladsl.Source

时间:2016-06-01 11:16:56

标签: scala akka slick

我刚刚开始在Scala(2.11.7)中使用akka streams(2.4.6)和slick(3.1.1)。我正在使用Intellij(不确定这是否重要,但我认为我会把它扔出去)。

val scanner: DatabasePublisher[Stage] = db.stream(action.transactionally.withStatementParameters(fetchSize = 5000))
val source = Source.fromPublisher(scanner)

问题出在第二行。 Intellij告诉我它无法解析符号" fromPublisher" ......我以为是IJ很顽皮,但是当我去修建时,我得到了这个:

Error:(38, 10) value fromPublisher is not a member of object akka.stream.scaladsl.Source
  Source.fromPublisher(scanner)
         ^

任何我可能做错的想法?

整晚都在和我斗争,我准备把头发拉出来。

谢谢!

2 个答案:

答案 0 :(得分:1)

参见工作示例:

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Source
import slick.backend.DatabasePublisher
import slick.driver.H2Driver.api._

import scala.concurrent.Await
import scala.concurrent.duration._

case class Emp(id: Int, name: String)

object Demo extends App {

  implicit val system = ActorSystem("Sys")

  val db = Database.forConfig("h2mem1")

  val empTableQuery = TableQuery[EmployeeTable]
  val insertQuery = empTableQuery ++= Seq(Emp(1, "emp1"), Emp(2, "emp2"), Emp(3, "emp3"), Emp(4, "emp4"))
  val action = DBIO.seq(empTableQuery.schema.create, insertQuery)

  //create schema and insert record
  Await.result(db.run(action), 1000 second)
  // print db record
  Await.result(db.run(empTableQuery.result), 1000 second).foreach(println)
  val publisher: DatabasePublisher[Emp] = db.stream(empTableQuery.result)

  import system.dispatcher

  implicit val materializer = ActorMaterializer()

  //consume using stream
  println("Steaming data::::::::")
  val source = Source.fromPublisher(publisher).map(emp => emp.id + " : " + emp.name).runForeach(println)

  class EmployeeTable(tag: Tag) extends Table[Emp](tag, "emp") {
    val id = column[Int]("id", O.PrimaryKey)
    val name = column[String]("name")

    def * = (id, name) <>(Emp.tupled, Emp.unapply)
  }


  source.onComplete(_ => system.terminate())

}

<强> build.sbt

   scalaVersion := "2.11.8"

libraryDependencies ++= Seq(
  "mysql" % "mysql-connector-java" % "5.1.36",
  "com.typesafe.slick" %% "slick-hikaricp" % "3.1.1",
  "ch.qos.logback" % "logback-classic" % "1.1.3",
  "com.typesafe.slick" %% "slick" % "3.1.1",
  "com.typesafe.akka" %% "akka-stream" % "2.4.6",
  "org.scalatest" %% "scalatest" % "2.2.5" % "test",
  "com.h2database" % "h2" % "1.4.187"
)

<强> application.conf

h2mem1 = {
  url = "jdbc:h2:mem:test1"
  driver = org.h2.Driver
  connectionPool = disabled
  keepAliveConnection = true
}

答案 1 :(得分:0)

谢谢大家回复我的问题。我发现了这个问题。这不是代码。

问题出在Intellij库设置中。

我用SBT编译了这个项目,尽管事实上IJ正在全力以赴,但它仍然运行良好。

所以我在戳了一下之后,我进入了项目结构 - &gt; SDK和我注意到在SDK库中,akka-stream-experimental 1.0 jar和一大堆其他垃圾。

我删除了那些(留下了JDK 8罐)。现在一切正常!

无论如何,谢谢!