package com.bayakala.funda.fdasources.examples import slick.driver.H2Driver.api._ import com.bayakala.funda.fdasources.FDADataStream._ import com.bayakala.funda.samples._ import com.bayakala.funda.fdarows._ import com.bayakala.funda.fdapipes._ import FDANodes._ import FDAValves._ object Example2 extends App { val albums = SlickModels.albums val companies = SlickModels.companies //数据源query
val albumsInfo = for { (a,c) <- albums join companies on (_.company === _.id) } yield (a.title,a.artist,a.year,c.name) //query结果强类型(用户提供)
case class Album(title: String, artist: String, year: Int, publisher: String) extends FDAROW //转换函数(用户提供)
def toTypedRow(row: (String, String, Option[Int], String)): Album = Album(row._1, row._2, row._3.getOrElse(2000), row._4) val db = Database.forConfig("h2db") val streamLoader = FDAStreamLoader(slick.driver.H2Driver, toTypedRow _) val albumStream = streamLoader.fda_typedStream(albumsInfo.result)(db)(512,128) //定义一个用户作业函数:列印数据内容
def printAlbums: FDATask[FDAROW] = row => { row match { case album: Album => println("____________________") println(s"品名:${album.title}") println(s"演唱:${album.artist}") println(s"年份:${album.year}") println(s"发行:${album.publisher}") fda_next(album) case _ => fda_skip } } albumStream.through(fda_execUserTask(printAlbums)).run.unsafeRun }
品名:Keyboard Cat's Greatest Hits
演唱:Keyboard Cat 年份:1999 发行:Sony Music Inc ____________________ 品名:Spice 演唱:Spice Girls 年份:1999 发行:Columbia Records ____________________ 品名:Whenever You Need Somebody 演唱:Rick Astley 年份:1999 发行:Sony Music Inc ____________________ 品名:The Triumph of Steel 演唱:Manowar 年份:1999 发行:The K-Pops Singers ____________________ 品名:Believe 演唱:Justin Bieber 年份:1999 发行:Columbia Records Process finished with exit code 0