Spring Batch 3 FlatFileItemReader and FlatFileItemWriter Annotation Example

By Arvind Rai, November 08, 2014
This page is describing the use of FlatFileItemReader and FlatFileItemWriter using annotation in spring bath 3. We will create the bean for FlatFileItemReader and FlatFileItemWriter. Flat file resources can be read and written using ClassPathResource or FileSystemResource. If using ClassPathResource, we need to put file in classpath and if using FileSystemResource we can set even absolute path.

Project Configuration in Eclipse

Find the project structure in eclipse.
Spring Batch 3  FlatFileItemReader and FlatFileItemWriter  Annotation Example

Configuration File for FlatFileItemReader and FlatFileItemWriter

Find the configuration file for FlatFileItemReader and FlatFileItemWriter.
BatchConfiguration.java
package com.concretepage.springbatch;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor;
import org.springframework.batch.item.file.transform.DelimitedLineAggregator;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
    @Bean
    public ItemReader<Student> reader() {
        FlatFileItemReader<Student> reader = new FlatFileItemReader<Student>();
        reader.setResource(new ClassPathResource("student-data.csv"));
        reader.setLineMapper(new DefaultLineMapper<Student>() {{
            setLineTokenizer(new DelimitedLineTokenizer() {{
                setNames(new String[] {"stdId", "subMarkOne", "subMarkTwo" });
            }});
            setFieldSetMapper(new BeanWrapperFieldSetMapper<Student>() {{
                setTargetType(Student.class);
            }});
        }});
        return reader;
    }
    @Bean
    public ItemWriter<Marksheet> writer() {
    	FlatFileItemWriter<Marksheet> writer = new FlatFileItemWriter<Marksheet>();
    	writer.setResource(new ClassPathResource("student-marksheet.csv"));
    	DelimitedLineAggregator<Marksheet> delLineAgg = new DelimitedLineAggregator<Marksheet>();
    	delLineAgg.setDelimiter(",");
    	BeanWrapperFieldExtractor<Marksheet> fieldExtractor = new BeanWrapperFieldExtractor<Marksheet>();
    	fieldExtractor.setNames(new String[] {"stdId", "totalSubMark"});
    	delLineAgg.setFieldExtractor(fieldExtractor);
    	writer.setLineAggregator(delLineAgg);
        return writer;
    }
    @Bean
    public ItemProcessor<Student, Marksheet> processor() {
        return new StudentItemProcessor();
    }
    @Bean
    public Job createMarkSheet(JobBuilderFactory jobs, Step step) {
        return jobs.get("createMarkSheet")
                .flow(step)
                .end()
                .build();
    }
    @Bean
    public Step step(StepBuilderFactory stepBuilderFactory, ItemReader<Student> reader,
            ItemWriter<Marksheet> writer, ItemProcessor<Student, Marksheet> processor) {
        return stepBuilderFactory.get("step")
                .<Student, Marksheet> chunk(5)
                .reader(reader)
                .processor(processor)
                .writer(writer)
                .build();
    }
} 

Create Bean for FlatFileItemReader

To create FlatFileItemReader we need to do below steps.
1. Set Resource
We need to set flat file resource to read data from the file. To read resource we can use ClassPathResource or FileSystemResource etc.
2. Set LineMapper
We can use DefaultLineMapper to set in FlatFileItemReader instance. DefaultLineMapper has a method setLineTokenizer that needs to be defined. We can use DelimitedLineTokenizer here to set. Define a method setNames of DelimitedLineTokenizer. To define setNames method, we need to pass field names of input resource.
3. Set FieldSetMapper
Define BeanWrapperFieldSetMapper class. This class has a method setTargetType that need to be set. Target type is the class which is being used to read input.

Create Bean for FlatFileItemWriter

To create FlatFileItemWriter bean, find the below steps which are the setter methods that define the data required by FlatFileItemWriter .
1. Set Resource
Set a flat file resource for the output. We can use ClassPathResource or FileSystemResource etc. to get resource instance.
2. Set LineAggregator
In our example we are using DelimitedLineAggregator. Instantiate this class and set values for two methods, setDelimiter and setFieldExtractor. In setDelimiter method we set the token on the basis of which , values will be delimited. FieldExtractor consist the name of fields defined in the class which is representing output data.

Run the Example and check the Output

Download the complete source code from the download link on the bottom. Put the two input and output file in classpath as mentioned in configuration class. Use the below class to run the example.
Main.java
package com.concretepage.springbatch;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
@EnableAutoConfiguration
public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Main.class, args);
    }
} 
To check the output go to bin directory where we can get our output file.

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us