Learn to write CSV data using FlatFileItemWriter in a Spring batch application. The FlatFileItemWriter is an ItemWriter implementation that writes data to a flat file or stream. The location of the output file is defined by a Resource and must represent a writable file.
1. Maven
Include the Sprint batch starter module in the Spring boot application. Spring batch requires a database to store the Job execution information so we will use H2 database for the demo.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
2. Spring Batch FlatFileItemWriter
The FlatFileItemWriter class is specifically designed for writing data to a flat file in a batch-processing scenario. Flat files are files where each line represents a record, and the fields within the record are delimited by a specific character, such as a comma or tab (for example CSV files). It offers configuration options for defining the output file, specifying the format, and supporting optional features such as headers and footers.
The following is a simple FlatFileItemWriter configuration that writes the Person data to a file ‘outputData.csv‘.
- The BeanWrapperFieldExtractor classes take the field names of the model object (e.g. Person in our case) and extract the values from an instance to finally pass to DelimitedLineAggregator.
- The DelimitedLineAggregator arranges the field data in a flat-file format in which all fields are separated by a delimiter.
- Finally, the delimited data is written to a WriteableResource.
Resource resource = new FileSystemResource("c:/temp/outputData.csv");
@Bean
public FlatFileItemWriter<Person> writer() {
//Create writer instance
FlatFileItemWriter<Person> writer = new FlatFileItemWriter<>();
//Set output file location
writer.setResource(resource);
//All job repetitions should "append" to same output file
writer.setAppendAllowed(true);
//Name field values sequence based on object properties
writer.setLineAggregator(new DelimitedLineAggregator<>() {
{
setDelimiter(",");
setFieldExtractor(new BeanWrapperFieldExtractor<>() {
{
setNames(new String[]{"firstName", "lastName", "age", "active"});
}
});
}
});
return writer;
}
3. Output File Creation
When a new batch Job starts, it does the following:
- If the file does not exist then create a new file, and write data to it.
- If the file exists then write the data to it. It will overwrite any previous data written in the file. If you want to append the data in this file, the use writer.setAppendAllowed(true) method.
Also, we are using the same output file everytime and we do not want to duplicate the records, then we can use the write.shouldDeleteIfExists(true) method to delete the existing file (if there is one) every time a new Job is started. In case the Job is restarted due to a processing error, it won’t delete the file.
Drop me your questions in the comments section.
Happy Learning !!
Comments