简体   繁体   English

Spring Batch-用于不同分隔文件的FlatFileItemReader

[英]Spring Batch - FlatFileItemReader for different delimited files

I have two different files, one delimited by pipe separator "|" 我有两个不同的文件,一个用管道分隔符“ |”分隔 and one by comma ",". 还有一个用逗号“,”。

I am using Spring Batch to process these files using FlatFileItemReader. 我正在使用Spring Batch使用FlatFileItemReader处理这些文件。 I dont want to have two readers and two writers for the files. 我不想有两个读者和两个作家的文件。 Can I have somehow one generic FlatFileItemReader for both files? 我可以为这两个文件使用一个通用的FlatFileItemReader吗?

Also the objects to which the files will be mapped are different. 文件将映射到的对象也不同。

You can inject a DelimitedLineTokenizer and you can set the delimiter as per requirement. 您可以注入DelimitedLineTokenizer,并可以根据需要设置定界符。 you can make it generic using StepExecutionListener and need to override the beforeStep() method. 您可以使用StepExecutionListener使其通用,并需要覆盖beforeStep()方法。 You will set delimiter in StepExecution. 您将在StepExecution中设置定界符。 When you parse the file which are "," seperated then stepExecution.getExecutionContext().putString("delimiter", ","); 当您解析“,”文件时,则分别分隔stepExecution.getExecutionContext()。putString(“ delimiter”,“,”); and when file is seperated with "|" 当文件以“ |”分隔时 then stepExecution.getExecutionContext().putString("delimiter", "|"); 然后stepExecution.getExecutionContext()。putString(“ delimiter”,“ |”);

But you need to create two jobs. 但是您需要创建两个作业。 Need to specify listener accordingly. 需要相应地指定监听器。

You can see the example of above explain logic from Spring Batch on Walking Techie 您可以在Walking Techie上的Spring Batch中看到上面解释逻辑的示例
Code for generic Reader: 通用阅读器的代码:

@Bean
  @StepScope
  public FlatFileItemReader<Domain> reader(@Value("#{stepExecutionContext[delimiter]}") String delimiter) {
    FlatFileItemReader<Domain> reader = new FlatFileItemReader<>();
    reader.setResource(new ClassPathResource("sample-data.csv"));
    reader.setLineMapper(new DefaultLineMapper<Domain>() {{
      setLineTokenizer(new DelimitedLineTokenizer() {{
        setNames(new String[]{"id", "name"});
        setDelimiter(delimiter);
      }});
      setFieldSetMapper(new BeanWrapperFieldSetMapper<Domain>() {{
        setTargetType(Domain.class);
      }});
    }});
    return reader;
  }

You can find the many examples on spring batch in spring boot from Spring Batch Tutorial . 您可以从Spring Batch Tutorial中的spring boot中找到有关spring batch的许多示例。 You will find here all kind of problems related to spring batch. 您将在这里找到与弹簧批处理有关的所有问题。

You can inject a DelimitedLineTokenizer into the FlatFileItemReader and set the delimiter value to it. 您可以将DelimitedLineTokenizer注入FlatFileItemReader并将其定界符值设置为它。 Relevant parts of the XML config are as follows XML配置的相关部分如下

<bean id="pipeDelimitedFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
    ...
    <property name="lineMapper">
        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
            <property name="lineTokenizer">
                <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    ...
                    <property name="delimiter" value="|"/>
                </bean>
            </property>
            ...
        </bean>
    </property>
    ...
</bean>

You can eg similarly configure another, commaDelimitedFileItemReader bean (comma is actually the default delimiter value for DelimitedLineTokenizer) 例如,您可以类似地配置另一个commaDelimitedFileItemReader bean(逗号实际上是DelimitedLineTokenizer的默认分隔符值)

I have created a sample spring batch programme in spring boot that will create two jobs one job will handle to read data from CSV file which is separated by the comma and other jobs to read data from CSV file which is separated by the pipe("|"). 我已经在Spring Boot中创建了一个示例Spring Batch程序,该程序将创建两个作业,一个作业将处理以逗号分隔的CSV文件中的数据读取,其他作业将以竖线分隔的CSV文件中的数据读取(“ | ”)。 Both jobs are using the same common FlatFileItemReader to read data from CSV files and MongoItemWriter to write data into MongoDB. 这两个作业都使用相同的通用FlatFileItemReader从CSV文件读取数据,并使用MongoItemWriter将数据写入MongoDB。

You can find explanation and working code from Spring Batch Example in Spring Boot - CSV Files with different delimiter to Mongo Database 您可以在Spring Boot的Spring Batch示例中找到说明和工作代码-带有与Mongo数据库不同的分隔符的CSV文件

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

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