CsvReactiveIteratee - Scala and Play2
The following case class allow a Play2 controller to process a big CSV File upload reactively without putting the whole file in memory.
The function used to process each line is passed as a parameter.
My code is inspired by the following post : http://stackoverflow.com/questions/11941102/play-2-scala-best-way-to-upload-a-big-csv-file-with-iteratee-in-order-to-proce
package intech.api
import play.api.mvc._
import play.api.libs.iteratee.{Iteratee, Input}
import play.api.libs.concurrent.Promise
import play.api.libs.iteratee.Input.{El, EOF, Empty}
import au.com.bytecode.opencsv.CSVReader
import java.io.StringReader
import scala.collection.JavaConversions._
case class CsvReactiveIteratee(f: Array[String] => Int, state: Symbol = 'Cont, input: Input[Array[Byte]] = Empty, lastChunk: String = "", lineProcessed: Int = 0, isFirst: Boolean = false) extends Iteratee[Array[Byte], Either[Result, Int]] {
def fold[B](
done: (Either[Result, Int], Input[Array[Byte]]) => Promise[B],
cont: (Input[Array[Byte]] => Iteratee[Array[Byte], Either[Result, Int]]) => Promise[B],
error: (String, Input[Array[Byte]]) => Promise[B]
): Promise[B] = state match {
case 'Done =>
done(Right(lineProcessed), Input.Empty)
case 'Cont => cont(in => in match {
case in: El[Array[Byte]] => {
// Retrieve the part that has not been processed in the previous chunk and copy it in front of the current chunk
val content = lastChunk + new String(in.e)
val csvBody =
if (isFirst)
// Skip http header if it is the first chunk
content.drop(content.indexOf("\r\n\r\n") + 4)
else content
val csv = new CSVReader(new StringReader(csvBody), ';')
val lines = csv.readAll
// Process all lines excepted the last one since it is cut by the chunk
var nbLine = 0
for (line <- lines.init)
nbLine += f(line)
// Put forward the part that has not been processed
val last = lines.last.toList.mkString(";")
copy(input = in, lastChunk = last, lineProcessed = lineProcessed + nbLine, isFirst = false)
}
case Empty => copy(input = in, lineProcessed = lineProcessed, isFirst = false)
case EOF => copy(state = 'Done, input = in, lineProcessed = lineProcessed, isFirst = false)
case _ => copy(state = 'Error, input = in, lineProcessed = lineProcessed, isFirst = false)
})
case _ =>
error("Unexpected state", input)
}
}
The following controller uses the class to send a new json structure to elasticsearch for each line :
def upload = Action(BodyParser(rh => new CsvReactiveIteratee(f = processNALine, isFirst = true))) {
request =>
Ok("File Processed\n Nb lines processed : " + request.body)
}
def processNALine(line: Array[String]): Int =
if (line.size == 4) {
WS.url("http://localhost:9200/affa/na/").post(
toJson(
Map(
"date" -> toJson(line(0)),
"trig" -> toJson(line(1)),
"code" -> toJson(line(2)),
"nbs" -> toJson(line(3).toDouble)
)
)
)
1
} else 0
Written by Fabrice Croiseaux
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Scala
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#