Search in sources :

Example 11 with DefaultLineMapper

use of org.springframework.batch.item.file.mapping.DefaultLineMapper 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 12 with DefaultLineMapper

use of org.springframework.batch.item.file.mapping.DefaultLineMapper 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)

Example 13 with DefaultLineMapper

use of org.springframework.batch.item.file.mapping.DefaultLineMapper in project pkslow-samples by LarryDpk.

the class JobConfig method reader.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Bean
public FlatFileItemReader<Employee> reader() {
    // Create reader instance
    FlatFileItemReader<Employee> reader = new FlatFileItemReader<Employee>();
    // Set number of lines to skips. Use it if file has header rows.
    reader.setLinesToSkip(1);
    // Configure how each line will be parsed and mapped to different values
    reader.setLineMapper(new DefaultLineMapper() {

        {
            // 3 columns in each row
            setLineTokenizer(new DelimitedLineTokenizer() {

                {
                    setNames(new String[] { "id", "firstName", "lastName" });
                }
            });
            // Set values in Employee class
            setFieldSetMapper(new BeanWrapperFieldSetMapper<Employee>() {

                {
                    setTargetType(Employee.class);
                }
            });
        }
    });
    return reader;
}
Also used : FlatFileItemReader(org.springframework.batch.item.file.FlatFileItemReader) DelimitedLineTokenizer(org.springframework.batch.item.file.transform.DelimitedLineTokenizer) Employee(com.pkslow.batch.entity.Employee) DefaultLineMapper(org.springframework.batch.item.file.mapping.DefaultLineMapper) BeanWrapperFieldSetMapper(org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper) Bean(org.springframework.context.annotation.Bean)

Example 14 with DefaultLineMapper

use of org.springframework.batch.item.file.mapping.DefaultLineMapper in project invesdwin-nowicket by invesdwin.

the class CsvItemReaderBuilder method newDefaultLineMapper.

private DefaultLineMapper<T> newDefaultLineMapper() {
    final DefaultLineMapper<T> lineMapper = new DefaultLineMapper<T>();
    final DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
    if (delimiter != null) {
        tokenizer.setDelimiter(delimiter);
    }
    if (names != null) {
        tokenizer.setNames(names);
    }
    if (strict != null) {
        tokenizer.setStrict(strict);
    }
    if (fieldSetFactory != null) {
        tokenizer.setFieldSetFactory(fieldSetFactory);
    }
    lineMapper.setLineTokenizer(tokenizer);
    if (fieldSetMapper != null) {
        lineMapper.setFieldSetMapper(fieldSetMapper);
    }
    lineMapper.afterPropertiesSet();
    return lineMapper;
}
Also used : DelimitedLineTokenizer(org.springframework.batch.item.file.transform.DelimitedLineTokenizer) DefaultLineMapper(org.springframework.batch.item.file.mapping.DefaultLineMapper)

Example 15 with DefaultLineMapper

use of org.springframework.batch.item.file.mapping.DefaultLineMapper in project invesdwin-context by invesdwin.

the class CsvItemReaderBuilder method newDefaultLineMapper.

private DefaultLineMapper<T> newDefaultLineMapper() {
    final DefaultLineMapper<T> lineMapper = new DefaultLineMapper<T>();
    final DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
    if (delimiter != null) {
        tokenizer.setDelimiter(delimiter);
    }
    if (names != null) {
        tokenizer.setNames(names);
    }
    if (strict != null) {
        tokenizer.setStrict(strict);
    }
    if (fieldSetFactory != null) {
        tokenizer.setFieldSetFactory(fieldSetFactory);
    }
    lineMapper.setLineTokenizer(tokenizer);
    if (fieldSetMapper != null) {
        lineMapper.setFieldSetMapper(fieldSetMapper);
    }
    lineMapper.afterPropertiesSet();
    return lineMapper;
}
Also used : DelimitedLineTokenizer(org.springframework.batch.item.file.transform.DelimitedLineTokenizer) DefaultLineMapper(org.springframework.batch.item.file.mapping.DefaultLineMapper)

Aggregations

DefaultLineMapper (org.springframework.batch.item.file.mapping.DefaultLineMapper)29 DelimitedLineTokenizer (org.springframework.batch.item.file.transform.DelimitedLineTokenizer)29 FlatFileItemReader (org.springframework.batch.item.file.FlatFileItemReader)18 FileSystemResource (org.springframework.core.io.FileSystemResource)13 Bean (org.springframework.context.annotation.Bean)10 ClassPathResource (org.springframework.core.io.ClassPathResource)7 JobRepositoryFactoryBean (org.springframework.batch.core.repository.support.JobRepositoryFactoryBean)5 CVRClinicalRecord (org.cbioportal.cmo.pipelines.cvr.model.staging.CVRClinicalRecord)3 Before (org.junit.Before)3 TaxonService (org.powo.api.TaxonService)3 ConversionService (org.springframework.core.convert.ConversionService)3 HashSet (java.util.HashSet)2 Transaction (org.baeldung.batch.model.Transaction)2 RecordFieldSetMapper (org.baeldung.batch.service.RecordFieldSetMapper)2 CVRFusionRecord (org.cbioportal.cmo.pipelines.cvr.model.staging.CVRFusionRecord)2 MskimpactSeqDate (org.cbioportal.cmo.pipelines.cvr.model.staging.MskimpactSeqDate)2 StringToIsoDateTimeConverter (org.powo.model.convert.StringToIsoDateTimeConverter)2 StepScope (org.springframework.batch.core.configuration.annotation.StepScope)2 BeanWrapperFieldSetMapper (org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper)2 ConversionServiceFactoryBean (org.springframework.context.support.ConversionServiceFactoryBean)2