spring batch -dynamic csv reader,具有不同的列

时间:2018-01-22 20:31:45

标签: java spring logic spring-batch

我正在使用spring批处理来处理CSV文件。

sample.csv

 fullname,dateOfBirth,  gender, nationality
 Joe Smith,09/11/1966, M, America
 William Norman,22/22/1988,M, Cuba

changed_csv.csv

 fullname,dateOfBirth,  gender, nationality,currency,regions
 Joe Smith,09/11/1966, M, America,Dollar, North America
 William Norman,22/22/1988,M, Cuba,Peso, South America

我的读者

enter code here .   


 private FlatFileItemReader<Person> reader(String filename) {
        FlatFileItemReader<Person> reader = new FlatFileItemReader<>();
        reader.setLinesToSkip(1);
        reader.setStrict(false);
        log.debug("Job parameter {}", filename);
        reader.setResource(new FileSystemResource(filename));
        reader.setRecordSeparatorPolicy(new DefaultRecordSeparatorPolicy());
        reader.setLineMapper(new PersonLineMapper());
        return reader;
    }
enter code here .   
public PersonLineMapper() {
    super();
    DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer();
    lineTokenizer.setNames(  "fullname","dateOfBirth",  "gender", "natinlaity");
    setLineTokenizer(lineTokenizer);
    BeanWrapperFieldSetMapper<Person> beanWrapperFieldSetMapper = new BeanWrapperFieldSetMapper<>();
    beanWrapperFieldSetMapper.setTargetType(Person.class);
    beanWrapperFieldSetMapper.setCustomEditors(Collections.singletonMap("java.time.LocalDate", new LocalDatePropertyEditor()));
    setFieldSetMapper(beanWrapperFieldSetMapper);
}

一切正常。

我的问题是有没有办法动态映射csv到bean。 就像我的csv引入了新的标题,如何处理它们

0 个答案:

没有答案