Created
May 7, 2012 01:18
-
-
Save asmuth/2625281 to your computer and use it in GitHub Desktop.
csv reader :)
This file contains hidden or 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 com.dawanda.recommender | |
import scala.collection.JavaConverters._ | |
import scala.collection.mutable.Buffer | |
import scala.io._ | |
import java.util.concurrent._ | |
class CSVReader[T <: Any](next: (Map[Symbol, Int] => T)){ | |
def read(file_path: String) : Buffer[T] = { | |
val src = Source.fromFile(file_path) | |
var hdl : List[Symbol] = null | |
val res = new java.util.LinkedList[Future[T]]() | |
val run = Executors.newFixedThreadPool(Recommender.num_threads) | |
(src.getLines().zipWithIndex foreach ((l: (String, Int)) => { | |
if (l._2 == 0) | |
hdl = parse_line(l._1) map (Symbol(_)) | |
else if (hdl != null) | |
res.add(run.submit(new Callable[T] { def call = { | |
next(parse_line_with_headers(l._1, hdl)) | |
}})) | |
})) | |
src.close() | |
run.shutdown | |
run.awaitTermination(Math.MAX_LONG, TimeUnit.SECONDS) | |
res.asScala.map(_.get(0, TimeUnit.SECONDS)) | |
} | |
private def parse_line(line: String) : List[String] = | |
line.split(",").toList map (_.replaceAll("^\"(.*)\"$", "$1")) // FIXPAUL HACK!!! :) | |
private def parse_line_with_headers(line: String, hdl: List[Symbol]) : Map[Symbol, Int] = | |
Map((hdl.zip(parse_line(line)) map ( | |
(l: (Symbol, String)) => (l._1, Integer.parseInt(l._2)) | |
)).toArray: _*) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment