2012-08-13 8 views
11

非常に大きなCSVファイル(数百万行)をelasticsearchにアップロードするにはPlay2を使用します。私はうまく動作する次のコードを書いている。Play 2 Scala - 各行を反応的に処理するためにIterateeで大きなCSVファイルをアップロードする最も良い方法

最初のチャンクでhttp応答ヘッダーをスキップする方法には満足していません。 httpヘッダーをスキップしてDone状態に直接切り替える最初のIterateeでこのiterateeをチェーンする方法があるはずですが、どのように見つけられていません。

誰もが2 iteratees一緒にチェーンに

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) 
    ) 
    ) 
) 
} 
+1

:あなたが好きな何かをしたい場合があります

-iterate-extras/blob/master/src/main/scala/play/extras/iteratees/Csv.scala)を参照してください。 –

答えて

1

を助けることができる場合は、flatMapを使用しています。

val combinedIteratee = firstIteratee.flatMap(firstResult => secondIteratee) 

か、理解のために使用した:

val combinedIteratee = for { 
    firstResult <- firstIteratee 
    secondResult <- secondIteratee 
} yield secondResult 

あなたが好きなようにあなたが一緒にできるだけ多くiterateesを配列決定するflatMapを使用することができます。あなたは[CSVパーサ](https://github.com/jroper/playを持っている、非公式プレイiteratee-エキストラに興味がありますによって

val headerAndCsvIteratee = for { 
    headerResult <- headerIteratee 
    csvResult <- csvIteratee 
} yield csvResult 
関連する問題