Tony Edwards
Tony Edwards

Reputation: 427

How do I store the whole read line using Spring Batch?

I am using Spring Boot 1.4 and Spring Batch 1.4 to read in a file and of course, parse the data into the database.

What I would like to do is store the entire line read in the database before the fields are mapped. The entire row would be stored as a string in the database. This is for auditing purposes, therefore I do not want to rebuild the row string from its components.

We have all seen the common mappers in use to get the data from the delimited line:

@Bean
@StepScope
public FlatFileItemReader<Claim> claimFileReader(@Value("#{jobParameters[fileName]}") String pathToFile) {
    logger.debug("Setting up FlatFileItemReader for claim");
    logger.debug("Job Parameter for input filename: " + pathToFile);

    FlatFileItemReader<Claim> reader = new FlatFileItemReader<Claim>();
    reader.setResource(new FileSystemResource(pathToFile));
    reader.setLineMapper(claimLineMapper());
    logger.debug("Finished setting up FlatFileItemReader for claim");
    return reader;
}

@Bean
public LineMapper<Claim> claimLineMapper() {
    logger.debug("Setting up lineMapper");

    DefaultLineMapper<Claim> lineMapper = new DefaultLineMapper<Claim>();

    DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer();
    lineTokenizer.setDelimiter("|");
    lineTokenizer.setStrict(false);
    lineTokenizer.setNames(new String[] { "RX_NUMBER", "SERVICE_DT", "CLAIM_STS", "PROCESSOR_CLAIM_ID", "CARRIER_ID", "GROUP_ID", "MEM_UNIQUE_ID" });

    BeanWrapperFieldSetMapper<Claim> fieldSetMapper = new BeanWrapperFieldSetMapper<Claim>();
    fieldSetMapper.setTargetType(Claim.class);

    lineMapper.setLineTokenizer(lineTokenizer);
    lineMapper.setFieldSetMapper(claimFieldSetMapper());

    logger.debug("Finished Setting up lineMapper");

    return lineMapper;
}

If this is my row: 463832|20160101|PAID|504419000000|XYZ|GOLD PLAN|561868

I would want to store "463832|20160101|PAID|504419000000|HBT|GOLD PLAN|561868" as the string in the database (probably with some additional data such as job_instance_id).

Any ideas on how to hook this in during the file reading process?

Upvotes: 2

Views: 4356

Answers (1)

Nghia Do
Nghia Do

Reputation: 2658

Instead of using DefaultLineMapper, you can have a new class (suggest CustomLineMapper) as below

public class CustomLineMapper extends DefaultLineMapper<Claim> {

    @Override
    public Claim mapLine(String line, int lineNumber) throws Exception {
        // here you can handle *line content*
        return super.mapLine(line, lineNumber);
    }
}

line object will contains the raw data which is before mapping it to an object.

Upvotes: 4

Related Questions