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 batch | |
import scala.xml.Elem | |
trait ItemReader[A] { | |
def read: Option[A] | |
def hasNext: Boolean | |
} | |
trait ItemProcessor[A,B] { | |
def process(x: A): B | |
} | |
trait ItemWriter[B] { | |
def write(items: List[B]): Unit | |
} | |
trait BatchProcessor[S,T] { | |
val itemReader: ItemReader[S] | |
val itemProcessor: ItemProcessor[S,T] | |
val itemWriter: ItemWriter[T] | |
def execute: Unit = { | |
while(itemReader.hasNext) { | |
for (item <- itemReader.read){ | |
itemWriter.write(List(itemProcessor.process(item))) | |
} | |
} | |
} | |
} | |
class ListReader[A](list: List[A]) extends ItemReader[A] { | |
val iterator:Iterator[A] = list.iterator | |
override def read: Option[A] = { | |
new Some(iterator.next) | |
} | |
override def hasNext: Boolean = { | |
iterator.hasNext | |
} | |
} | |
/** | |
* Our simple batchprogram will | |
* - read cities one by one from a list of cities | |
* - process them (transform from scala to xml) | |
* - write each list containing 1 city to the console | |
* | |
* I left out chunking for simplicity | |
*/ | |
object BatchProgram { | |
case class City(name: String, population: Long) { | |
def marshal: Elem = { | |
<city> | |
<name>{name}</name> | |
<population>{population}</population> | |
</city> | |
} | |
} | |
val cities = List(City("Paris", 2234000000L), City("London", 8174000000L)) | |
val cityReader: ListReader[City] = new ListReader(cities) | |
val cityProcessor:ItemProcessor[City,Elem] = new ItemProcessor[City,Elem]() { | |
override def process(city: City): Elem = { | |
city.marshal | |
} | |
} | |
val cityWriter: ItemWriter[Elem] = new ItemWriter[Elem]() { | |
override def write(items: List[Elem]): Unit = { | |
println("*****************BATCH START*************") | |
for (item <- items) println(item) | |
println("*****************BATCH END***************") | |
} | |
} | |
val batchProcessor: BatchProcessor[City, Elem] = new { | |
val itemReader = cityReader | |
val itemProcessor = cityProcessor | |
val itemWriter = cityWriter | |
} with BatchProcessor[City, Elem] | |
def main(args: Array[String]) { | |
batchProcessor.execute | |
} | |
} | |
/** | |
This will print the following to the console: | |
*****************BATCH START************* | |
<city> | |
<name>Paris</name> | |
<population>2234000000</population> | |
</city> | |
*****************BATCH END*************** | |
*****************BATCH START************* | |
<city> | |
<name>London</name> | |
<population>8174000000</population> | |
</city> | |
*****************BATCH END*************** | |
**/ |
No comments:
Post a Comment