Reputation: 55
My Spring Batch job reads a list of csv
files containing two types of headers. I want the reader to skip the entire file if its header does not match one of the two possible header types.
I've taken a look at Spring Boot batch - MultiResourceItemReader : move to next file on error. But I don't see how to validate the header tokens to ensure they match up in count and content
Upvotes: 0
Views: 936
Reputation: 55
I was able to figure this out by doing the following,
public FlatFileItemReader<RawFile> reader() {
return new FlatFileItemReaderBuilder<RawFile>()
.skippedLinesCallback(line -> {
// Verify file header is what we expect
if (!StringUtils.equals(line, header)) {
throw new IllegalArgumentException(String.format("Bad header!", line));
}
})
.name( "myReader" )
.linesToSkip( 1 )
.lineMapper( new DefaultLineMapper() {
{
setLineTokenizer( lineTokenizer );
setFieldSetMapper( fieldSetMapper );
}} )
.build();
}
I call the reader()
method when setting the delegate in my MultiResourceItemReader
.
Note that header
, lineTokenizer
, and fieldSetMapper
are all variables that I set depending on which type of file (and hence which set of headers) my job is expected to read.
Upvotes: 1