Reputation: 749
I have a csv file which I need to process and insert data in database which is very simple job which I did, but now my problem is before inserting data in database I need to check if first line of csv file having header or not if not discard the file e.g. first line has to be HDR|DEPT|DATE last line - TLR|NumberOfRows if these first and last line doesn't exist ignore this file,
Upvotes: 0
Views: 3230
Reputation: 4454
Simply write a TaskletStep as first step. Open the file, read and check the first line, then read line by line till you found the last line, check it.
Throw an appropriate exception if the first and last line do not match the expectations.
You can not simply use header and footer callbacks of SpringBatch FlatFileItemReader, since the footer callback is called after all files have been processed.
EDIT: added example
Something like this:
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public Job job() throws Exception {
return this.jobBuilderFactory.get(JOB_NAME)
.start(validateStep())
.next(yourNormalStepGoesHere())
.build();
}
@Bean
protected Step validateStep() throws Exception {
return this.stepBuilderFactory.get("SimpleTest_step1_Step")
.tasklet(tasklet())
.build();
}
@Bean
protected Tasklet tasklet() {
return (contribution, context) -> {
// open file
// read first line
// check first line
// read lines until last line is read
// check last line
// close file
return RepeatStatus.FINISHED;
};
}
Upvotes: 2