Skip to content

Instantly share code, notes, and snippets.

@satendrakumar
Last active May 13, 2017 11:12
Show Gist options
  • Save satendrakumar/2fae274dc08bbb8c382631438de211d9 to your computer and use it in GitHub Desktop.
Save satendrakumar/2fae274dc08bbb8c382631438de211d9 to your computer and use it in GitHub Desktop.
Parse CSV using univocity parser
import com.univocity.parsers.csv.{CsvParser, CsvParserSettings}
class CSVParser(delimiter: Char = ',') {
private val parser = {
val settings = new CsvParserSettings
val format = settings.getFormat
format.setLineSeparator("\n")
format.setDelimiter(delimiter)
format.setQuote('"')
format.setQuoteEscape('\\')
format.setComment('\0')
settings.setIgnoreLeadingWhitespaces(true)
settings.setIgnoreTrailingWhitespaces(true)
settings.setReadInputOnSeparateThread(false)
settings.setEmptyValue("")
settings.setMaxCharsPerColumn(-1)
settings.setMaxColumns(20000)
new CsvParser(settings)
}
def parse(line: String): Array[String] = {
parser.parseLine(line)
}
}
//sbt dependency
// "com.univocity" % "univocity-parsers" % "2.4.1",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment