我正在尝试在 spring 批处理的 reader() 步骤中配置 openCSV,以直接将从 CSV 文件读取的记录转换为 JAVA POJO。但我遇到了如何正确设置lineMapper
openCSV 的问题。
正如此处链接的帖子中所建议的,如何在春季批处理中将 flatFileItemReader 替换为 openCSV,我正在尝试如下:
public Event reader() throws IOException {
FlatFileItemReader<Event> itemReader = new FlatFileItemReader<Event>();
itemReader.setLineMapper(lineMapper());
itemReader.setLinesToSkip(1);
itemReader.setResource(new FileSystemResource(inputFilePath));
return itemReader;
}
但我无法弄清楚如何配置 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(???);
我有读取文件并将其转换为所需的 POJO 的代码,但是将其放在哪里:
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();
}
非常感谢任何帮助我指出正确方向的帮助。