我想使用 Play2 在 elasticsearch 上上传一个非常大的 CSV 文件(数百万行)。我编写了以下代码,效果很好。
我对跳过第一个块中的 http 响应 header 的方式不满意。应该有一种方法可以将这个 iteratee 与跳过 http header 并直接切换到 Done 状态的第一个 Iteratee 链接起来,但我还没有找到方法。
如果有人可以帮忙
object ReactiveFileUpload extends Controller {
def upload = Action(BodyParser(rh => new CsvIteratee(isFirst = true))) {
request =>
Ok("File Processed")
}
}
case class CsvIteratee(state: Symbol = 'Cont, input: Input[Array[Byte]] = Empty, lastChunk: String = "", isFirst: Boolean = false) extends Iteratee[Array[Byte], Either[Result, String]] {
def fold[B](
done: (Either[Result, String], Input[Array[Byte]]) => Promise[B],
cont: (Input[Array[Byte]] => Iteratee[Array[Byte], Either[Result, String]]) => Promise[B],
error: (String, Input[Array[Byte]]) => Promise[B]
): Promise[B] = state match {
case 'Done =>
done(Right(lastChunk), 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
for (line <- lines.init)
processLine(line)
// Put forward the part that has not been processed
val last = lines.last.toList.mkString(";")
copy(input = in, lastChunk = last, isFirst = false)
}
case Empty => copy(input = in, isFirst = false)
case EOF => copy(state = 'Done, input = in, isFirst = false)
case _ => copy(state = 'Error, input = in, isFirst = false)
})
case _ =>
error("Unexpected state", input)
}
def processLine(line: Array[String]) = WS.url("http://localhost:9200/affa/na/").post(
toJson(
Map(
"date" -> toJson(line(0)),
"trig" -> toJson(line(1)),
"code" -> toJson(line(2)),
"nbjours" -> toJson(line(3).toDouble)
)
)
)
}
最佳答案
要将两个迭代器链接在一起,请使用 flatMap
.
val combinedIteratee = firstIteratee.flatMap(firstResult => secondIteratee)
或者,使用 a 来理解:
val combinedIteratee = for {
firstResult <- firstIteratee
secondResult <- secondIteratee
} yield secondResult
您可以使用 flatMap 将任意数量的迭代器排列在一起。
您可能想要执行以下操作:
val headerAndCsvIteratee = for {
headerResult <- headerIteratee
csvResult <- csvIteratee
} yield csvResult
关于scala - Play 2 Scala - 使用 Iteratee 上传大型 CSV 文件以便 react 性地处理每一行的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11941102/