Ramesh Sivaraman Ramesh Sivaraman - 2 months ago 7
Scala Question

Univocity parser - Iterator way generating scala case classes

Using Univocity framework to do custom parsing requirements. we have an iterator outputing each line as an event by calling parseNext(), we do not want to use "parse(File file)" .

We have this Scala case class as the final output but at the moment we are handling output from parser and using a factory class to create scala case classes.

Is there an iterator way to generate case class objects from univocity (I did find BeanListProcessor but which does not work for iterator way)?

Answer can be in Java or Scala..

Thanks,
R

def parseRecord(field: Array[String], univocityContext: Option[ParsingContext]): Option[lineEvent] = {

val parsingContext = univocityContext.get
val parsedEvent = new ParsedEventConstructor()

for ((index, counter) <- parsingContext.extractedFieldIndexes().zipWithIndex){

val columnHeader = parsingContext.headers()(index)

columnHeader match {

case "header1" => {
parsedEvent.parsedheader1 += field(counter)
}
case "header2" => {
parsedEvent.parsedheader2 += field(counter)
}
case _ => parsedEvent.parsedOtherValues += field(counter)
}
}

Some(parsedEvent.getParsedEvent())
}

Answer

uniVocity-parsers has a BeanProcessor (without "List" in the name) that will submit each parsed bean to a "beanProcessed" callback method you need to implement. The BeanListProcessor is just a convenience class that extends BeanProcessor to add each object into a list, it's not the only way to get the objects.

You can also use a CsvRoutines object and its iterate method to iterate over beans without using the callback mentioned above. Check this example.