I am trying o use Univocity Parsers within Spring Batch. The problem I am facing is how to integrate them.
Spring Batch Chunk Steps follow the flow for each row of the given file:
I need to use Univocity inside an ItemReader. It executes the read()
method for each row of the input file (ie. CSV File). The only thing I did was using a BeanListProcessor
to read and convert items directly to my Java Object returning a List
of the parsed Beans, but I do not want to load all the records at once, to avoid OutOfMemory
exceptions. I did not find anything else that could help me.
I have tried using this answer as an example, but could not figure out anything to return one item at a time.
@Override
public Address read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
CsvParserSettings parserSettings = new CsvParserSettings();
//settings
CsvRoutines routines = new CsvRoutines(parserSettings);
for (Address address : routines.iterate(Address.class, input, "UTF-8")) {
/*
*here I need to return only the current object,
*the return of this method will be passed as an argument to a processor
*the next time this method is called it has to return the next one
*Could not figure out how to control what is the current.
*/
return ???:
}
return ???;
}
How can I use Univocity inside my ItemReader reading one row at a time, still using a BeanProcessor to parse my rows automatically to my Java Object?