Search in sources :

Example 1 with RecordFieldSetMapper

use of org.baeldung.batch.service.RecordFieldSetMapper in project tutorials by eugenp.

the class SpringBatchConfig method itemReader.

@Bean
public ItemReader<Transaction> itemReader() throws UnexpectedInputException, ParseException {
    FlatFileItemReader<Transaction> reader = new FlatFileItemReader<Transaction>();
    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
    String[] tokens = { "username", "userid", "transactiondate", "amount" };
    tokenizer.setNames(tokens);
    reader.setResource(inputCsv);
    DefaultLineMapper<Transaction> lineMapper = new DefaultLineMapper<Transaction>();
    lineMapper.setLineTokenizer(tokenizer);
    lineMapper.setFieldSetMapper(new RecordFieldSetMapper());
    reader.setLinesToSkip(1);
    reader.setLineMapper(lineMapper);
    return reader;
}
Also used : RecordFieldSetMapper(org.baeldung.batch.service.RecordFieldSetMapper) FlatFileItemReader(org.springframework.batch.item.file.FlatFileItemReader) DelimitedLineTokenizer(org.springframework.batch.item.file.transform.DelimitedLineTokenizer) Transaction(org.baeldung.batch.model.Transaction) DefaultLineMapper(org.springframework.batch.item.file.mapping.DefaultLineMapper) Bean(org.springframework.context.annotation.Bean)

Example 2 with RecordFieldSetMapper

use of org.baeldung.batch.service.RecordFieldSetMapper in project tutorials by eugenp.

the class SpringbatchPartitionConfig method itemReader.

@Bean
@StepScope
public FlatFileItemReader<Transaction> itemReader(@Value("#{stepExecutionContext[fileName]}") String filename) throws UnexpectedInputException, ParseException {
    FlatFileItemReader<Transaction> reader = new FlatFileItemReader<>();
    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
    String[] tokens = { "username", "userid", "transactiondate", "amount" };
    tokenizer.setNames(tokens);
    reader.setResource(new ClassPathResource("input/partitioner/" + filename));
    DefaultLineMapper<Transaction> lineMapper = new DefaultLineMapper<>();
    lineMapper.setLineTokenizer(tokenizer);
    lineMapper.setFieldSetMapper(new RecordFieldSetMapper());
    reader.setLinesToSkip(1);
    reader.setLineMapper(lineMapper);
    return reader;
}
Also used : RecordFieldSetMapper(org.baeldung.batch.service.RecordFieldSetMapper) FlatFileItemReader(org.springframework.batch.item.file.FlatFileItemReader) DelimitedLineTokenizer(org.springframework.batch.item.file.transform.DelimitedLineTokenizer) Transaction(org.baeldung.batch.model.Transaction) DefaultLineMapper(org.springframework.batch.item.file.mapping.DefaultLineMapper) ClassPathResource(org.springframework.core.io.ClassPathResource) StepScope(org.springframework.batch.core.configuration.annotation.StepScope) JobRepositoryFactoryBean(org.springframework.batch.core.repository.support.JobRepositoryFactoryBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

Transaction (org.baeldung.batch.model.Transaction)2 RecordFieldSetMapper (org.baeldung.batch.service.RecordFieldSetMapper)2 FlatFileItemReader (org.springframework.batch.item.file.FlatFileItemReader)2 DefaultLineMapper (org.springframework.batch.item.file.mapping.DefaultLineMapper)2 DelimitedLineTokenizer (org.springframework.batch.item.file.transform.DelimitedLineTokenizer)2 Bean (org.springframework.context.annotation.Bean)2 StepScope (org.springframework.batch.core.configuration.annotation.StepScope)1 JobRepositoryFactoryBean (org.springframework.batch.core.repository.support.JobRepositoryFactoryBean)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1