简体   繁体   English

在 spring 批处理步骤中配置 openCSV 而不是 FlatFileItemReader

[英]Configuring openCSV instead of FlatFileItemReader in spring batch step

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.我正在尝试在 spring 批处理的 reader() 步骤中配置 openCSV,以将从 CSV 文件中读取的记录直接转换为 JAVA POJO。 But I am running into the issue of how to correctly set the lineMapper with the openCSV.但是我遇到了如何使用 openCSV 正确设置lineMapper的问题。

As suggested in the post linked here How to replace flatFileItemReader with openCSV in spring batch , I am trying as below:正如此处链接的帖子中所建议的如何在 spring 批处理中将 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;
    }

But I am not able to figure out how to configure the lineMapper:但我无法弄清楚如何配置 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 :我有读取文件并将其转换为所需的 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();
        }

Any help to point me in the right direction is highly appreciated.非常感谢任何帮助我指出正确方向的帮助。

You are using the DefaultLineMapper and trying to set a LineTokenizer and FieldSetMapper in it, but this is not what is mentioned in the link you shared.您正在使用DefaultLineMapper并尝试在其中设置LineTokenizerFieldSetMapper ,但这不是您共享的链接中提到的内容。

You need a custom implementation of the LineMapper interface that is based on OpenCSV:您需要基于 OpenCSV 的LineMapper接口的自定义实现:

public class OpenCSVLineMapper<T> implements LineMapper<T> {
    
    @Override
    public T mapLine(String line, int lineNumber) throws Exception {
        // TODO use OpenCSV to map a line to a POJO of type T
        return null;
    }
}

OpenCSV provides APIs to both read the file and map data to objects. OpenCSV 提供 API 来读取文件和 map 数据到对象。 You don't need the reading part as this will be done by the FlatFileItemReader from Spring Batch, you only need to use OpenCSV for the mapping part.您不需要读取部分,因为这将由 Spring Batch 中的FlatFileItemReader完成,您只需将 OpenCSV 用于映射部分。

Once this in place, you can set your OpenCSV based line mapper implementation on the FlatFileItemReader :完成后,您可以在FlatFileItemReader上设置基于 OpenCSV 的线映射器实现:

public FlatFileItemReader<Event> reader() throws IOException {
   FlatFileItemReader<Event> itemReader = new FlatFileItemReader<Event>();
   itemReader.setResource(new FileSystemResource(inputFilePath));
   itemReader.setLinesToSkip(1);
   itemReader.setLineMapper(new OpenCSVLineMapper<>());
   return itemReader;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM