So I have a CSV file in which I have several attributes of books, in face of bookName, authorName, yearOfPublishing, etc. In order to process the file I am implementing batch framework. The problem is that I want to read and parse only the authorName field, and my app doesn't recognise it, it only passes the first column of my CSV file into my authorName field. Here is my item reader and line mapper.
@Bean
public FlatFileItemReader<Author> reader(){
FlatFileItemReader<Author> itemReader = new FlatFileItemReader<>();
itemReader.setResource(new FileSystemResource("src/main/resources/BX-Books.csv"));
itemReader.setName("csvReader");
itemReader.setLinesToSkip(1);
itemReader.setLineMapper(lineMapper());
itemReader.setStrict(false);
return itemReader;
}
private LineMapper<Author> lineMapper() {
DefaultLineMapper<Author> lineMapper = new DefaultLineMapper<>();
DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer();
lineTokenizer.setDelimiter(";");
lineTokenizer.setStrict(false);
lineTokenizer.setNames("Book-Author");
BeanWrapperFieldSetMapper<Author> fieldSetMapper = new BeanWrapperFieldSetMapper<>();
fieldSetMapper.setTargetType(Author.class);
lineMapper.setLineTokenizer(lineTokenizer);
lineMapper.setFieldSetMapper(fieldSetMapper);
return lineMapper;
}
CodePudding user response:
THIS would be the way to do it.
That would be the method setIncludedFields
in class org.springframework.batch.item.file.transform.DelimitedLineTokenizer
in case the above link goes bad.