简体   繁体   English

如何创建通用 FlatFileItemReader 以读取具有不同标题的 CSV 文件?

[英]How to create a generic FlatFileItemReader to read CSV files with different headers?

I'm creating a job that will read and process different .csv files based on an input parameter.我正在创建一个作业,它将根据输入参数读取和处理不同的 .csv 文件。 There are 3 different types of .csv files with different headers.有 3 种不同类型的 .csv 文件,具有不同的标题。 I want to map each line of a file to a POJO using a generic FlatFileItemReader .我想使用通用FlatFileItemReader将文件的每一行映射到 POJO。

Each type of file will have its own POJO implementation, and all "File Specific POJOs" are subclassed from an abstract GenericFilePOJO .每种类型的文件都有自己的 POJO 实现,并且所有“文件特定的 POJO”都是从抽象的GenericFilePOJO子类化的。

A tasklet will first read the input parameter to decide which file type needs to be read, and construct a LineTokenizer with the appropriate header columns.一个 tasklet 将首先读取输入参数来决定需要读取哪种文件类型,并构造一个带有适当标题列的 LineTokenizer。 It places this information in the infoHolder for retrieval at the reader step.它将此信息放在 infoHolder 中,以便在阅读器步骤中检索。

@Bean
public FlatFileItemReader<GenericFilePOJO> reader() {
    FlatFileItemReader<RawFile> reader = new FlatFileItemReader<GenericFilePOJO>();
    reader.setLinesToSkip(1); // header

    reader.setLineMapper(new DefaultLineMapper() {
        {
            // The infoHolder will contain the file-specific LineTokenizer
            setLineTokenizer(infoHolder.getLineTokenizer());
            setFieldSetMapper(new BeanWrapperFieldSetMapper<GenericFilePOJO>() {
                {
                    setTargetType(GenericFilePOJO.class);
                }
            });
        }
    });
    return reader;
}

Can this reader handle the different File Specific POJOs despite returning the GenericFilePOJO ?尽管返回了GenericFilePOJO这个读者能否处理不同的文件特定 POJO ?

You wrote:你写了:

A tasklet will first read the input parameter to decide which file type needs to be read. tasklet将首先读取输入参数来决定需要读取哪种文件类型。

Because the tasklet or infoHolder knows about type of file you can implement the creation of specific FieldSetMapper instance.因为taskletinfoHolder知道文件类型,所以您可以实现特定FieldSetMapper实例的创建。

This is a demo example how it can be implemented:这是一个如何实现的演示示例:

public class Solution<T extends GenericFilePOJO> {
    private InfoHolder infoHolder = new InfoHolder();

    @Bean
    public FlatFileItemReader<T> reader()
    {
        FlatFileItemReader<T> reader = new FlatFileItemReader<T>();
        reader.setLinesToSkip(1);

        reader.setLineMapper(new DefaultLineMapper() {
            {
                setLineTokenizer(infoHolder.getLineTokenizer());
                setFieldSetMapper(infoHolder.getFieldSetMapper());
            }
        });
        return reader;
    }

    private class InfoHolder {
        DelimitedLineTokenizer getLineTokenizer() {
            return <some already existent logic>;
        }

        FieldSetMapper<T> getFieldSetMapper() {
            if (some condition for specific file POJO 1){
                return new BeanWrapperFieldSetMapper<T>() {
                    {
                        setTargetType(FileSpecificPOJO_1.class);
                    }
                };
            } else if (some condition for specific file POJO 2){
                return new BeanWrapperFieldSetMapper<T>() {
                    {
                        setTargetType(FileSpecificPOJO_2.class);
                    }
                };
            }
        }
    }
}

暂无
暂无

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

相关问题 Spring Batch:如何使用FlatFileItemReader读取CSV文件的页脚和验证 - Spring Batch : How to read footer of CSV file and validation using FlatFileItemReader Spring Batch-用于不同分隔文件的FlatFileItemReader - Spring Batch - FlatFileItemReader for different delimited files 如何在不复制代码的情况下从两个不同的CSV文件读取并创建两个不同的数组? - How can I read from two different CSV files and create two different arrays without duplicating code? 如何在Spring批处理中使用FlatFileItemReader忽略CSV中不需要的列 - How to ignore unwanted columns in CSV using FlatFileItemReader in spring batch 从Spark中具有不同标题的CSV文件形成DataFrame - Forming DataFrames from CSV files with different headers in Spark 使用FlatFileItemReader读取一个csv文件,遇到空列抛出异常 - Read a csv file using FlatFileItemReader, throwing an exception when encountering an empty column 使用 FlatFileItemReader 读取元素列表 - Use FlatFileItemReader to read a List of elements 如何读取CSV标头并将它们放入java中的列表中 - How to read CSV headers and get them in to a list in java Java:如何将不同数据类型和长度的 csv 文件读入 ArrayList&lt;Object&gt;? - Java: How to read csv files of different data types and lengths into an ArrayList< Object>? 如何在向文件中添加更多行的同时动态添加 CSV 标头 - How to dynamically add CSV headers while more lines are added to the files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM