How to read first few rows from CSV using univocity parser

1.2k Views Asked by At

How to stop parsing after reading few rows from CSV file using iterator/row processor in univocity parser?

Update #1

I tried the below code and I'm getting empty rows.

val parserSettings = new CsvParserSettings
parserSettings.detectFormatAutomatically()
parserSettings.setEmptyValue("")
parserSettings.setNumberOfRecordsToRead(numberOfRecordsToRead)

val parser = new CsvParser(parserSettings)
val input = new FileInputStream(path)
val rows = parser.parseAll(input)

Update #2

Before passing inputstream to parser, I was using Apache Tika to detect the MIME type of the file to detect whether the file is CSV.

new Tika().detect(input)

This was altering the inputstream. Due to that Univocity parser was unable to parse correctly.

1

There are 1 best solutions below

7
On BEST ANSWER

You have many different options:

  1. From your row processor just call context.stop().

  2. On the parser settings, you can set settings.setNumberOfRecordsToRead(10) to read 10 rows and stop.

  3. With the parser itself, call parser.stopParsing()

Hope this helps