CsvReactiveIteratee – Scala e Play2

A seguinte classe de caso permite que um controlador Play2 processe um grande upload de arquivo CSV reativamente sem colocar o arquivo inteiro na memória.
A função usada para processar cada linha é passada como um parâmetro.
Meu código é inspirado na seguinte postagem: 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("rnrn") + 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)


}

}

O controlador a seguir usa a classe para enviar uma nova estrutura json para elasticsearch para cada linha:

def upload = Action(BodyParser(rh => new CsvReactiveIteratee(f = processNALine, isFirst = true))) {
request
=>
Ok("File Processedn 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