I am trying to configure openCSV in the reader() step in the spring batch to directly convert a record read from a CSV file into a JAVA POJO. But I am running into the issue of how to correctly set the lineMapper
with the openCSV.
As suggested in the post linked here How to replace flatFileItemReader with openCSV in spring batch, I am trying as below:
public Event reader() throws IOException {
FlatFileItemReader<Event> itemReader = new FlatFileItemReader<Event>();
itemReader.setLineMapper(lineMapper());
itemReader.setLinesToSkip(1);
itemReader.setResource(new FileSystemResource(inputFilePath));
return itemReader;
}
But I am not able to figure out how to configure the lineMapper:
public LineMapper<Event> lineMapper() throws IOException {
DefaultLineMapper<Event> lineMapper = new DefaultLineMapper<Event>();
DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer("\t");
BeanWrapperFieldSetMapper<Event> fieldSetMapper = new BeanWrapperFieldSetMapper<Event>();
fieldSetMapper.setTargetType(Event.class);
lineMapper.setLineTokenizer(???);
lineMapper.setFieldSetMapper(???);
I have the code to read the file and convert it to the desired POJO but where to put it:
try (
Reader reader = Files.newBufferedReader(Paths.get(inputFilePath));
) {
CsvToBean<Event> csvToBean = new CsvToBeanBuilder(reader)
.withSkipLines(1)
.withType(Event.class)
.withIgnoreLeadingWhiteSpace(true)
.build();
return csvToBean.iterator().next();
}
Any help to point me in the right direction is highly appreciated.