Search in sources :

Example 1 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 2 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 3 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 4 with DefaultLineMapper

use of org.springframework.batch.item.file.mapping.DefaultLineMapper in project powop by RBGKew.

the class DescriptionParsingTest method setUp.

@Before
public final void setUp() throws Exception {
    String[] names = new String[] { "http://rs.tdwg.org/dwc/terms/taxonID", "http://purl.org/dc/terms/created", "http://purl.org/dc/terms/modified", "http://purl.org/dc/terms/description", "http://purl.org/dc/terms/type", "http://purl.org/dc/terms/references", "http://purl.org/dc/terms/identifier" };
    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
    tokenizer.setDelimiter(DelimitedLineTokenizer.DELIMITER_TAB);
    tokenizer.setNames(names);
    taxonService = createMock(TaxonService.class);
    conversionService = createMock(ConversionService.class);
    FieldSetMapper fieldSetMapper = new FieldSetMapper();
    fieldSetMapper.setConversionService(conversionService);
    fieldSetMapper.setFieldNames(names);
    fieldSetMapper.setDefaultValues(new HashMap<String, String>());
    fieldSetMapper.setTaxonService(taxonService);
    DefaultLineMapper<Description> lineMapper = new DefaultLineMapper<Description>();
    lineMapper.setFieldSetMapper(fieldSetMapper);
    lineMapper.setLineTokenizer(tokenizer);
    flatFileItemReader.setEncoding("UTF-8");
    flatFileItemReader.setLinesToSkip(0);
    flatFileItemReader.setResource(content);
    flatFileItemReader.setLineMapper(lineMapper);
    flatFileItemReader.afterPropertiesSet();
}
Also used : DelimitedLineTokenizer(org.springframework.batch.item.file.transform.DelimitedLineTokenizer) TaxonService(org.powo.api.TaxonService) Description(org.powo.model.Description) ConversionService(org.springframework.core.convert.ConversionService) FieldSetMapper(org.powo.job.dwc.description.FieldSetMapper) DefaultLineMapper(org.springframework.batch.item.file.mapping.DefaultLineMapper) Before(org.junit.Before)

Example 5 with DefaultLineMapper

use of org.springframework.batch.item.file.mapping.DefaultLineMapper in project powop by RBGKew.

the class TaxonParsingTest method setUp.

@Before
public final void setUp() throws Exception {
    String[] names = new String[] { "http://rs.tdwg.org/dwc/terms/taxonID", "http://rs.tdwg.org/dwc/terms/scientificName", "http://rs.tdwg.org/dwc/terms/scientificNameID", "http://rs.tdwg.org/dwc/terms/scientificNameAuthorship", "http://rs.tdwg.org/dwc/terms/taxonRank", "http://rs.tdwg.org/dwc/terms/taxonomicStatus", "http://rs.tdwg.org/dwc/terms/parentNameUsageID", "http://rs.tdwg.org/dwc/terms/acceptedNameUsageID", "http://rs.tdwg.org/dwc/terms/genus", "http://rs.tdwg.org/dwc/terms/subgenus", "http://rs.tdwg.org/dwc/terms/specificEpithet", "http://rs.tdwg.org/dwc/terms/infraspecificEpithet", "http://purl.org/dc/elements/1.1/identifier", "http://purl.org/dc/elements/1.1/modified", "http://purl.org/dc/elements/1.1/source" };
    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
    tokenizer.setDelimiter(DelimitedLineTokenizer.DELIMITER_TAB);
    tokenizer.setNames(names);
    taxonService = EasyMock.createMock(TaxonService.class);
    @SuppressWarnings("rawtypes") Set<Converter> converters = new HashSet<>();
    converters.add(new StringToIsoDateTimeConverter());
    converters.add(new TaxonomicStatusConverter());
    converters.add(new RankConverter());
    converters.add(new NomenclaturalStatusConverter());
    ConversionServiceFactoryBean factoryBean = new ConversionServiceFactoryBean();
    factoryBean.setConverters(converters);
    factoryBean.afterPropertiesSet();
    ConversionService conversionService = factoryBean.getObject();
    FieldSetMapper fieldSetMapper = new FieldSetMapper();
    fieldSetMapper.setFieldNames(names);
    fieldSetMapper.setDefaultValues(new HashMap<String, String>());
    fieldSetMapper.setConversionService(conversionService);
    DefaultLineMapper<Taxon> lineMapper = new DefaultLineMapper<Taxon>();
    lineMapper.setFieldSetMapper(fieldSetMapper);
    lineMapper.setLineTokenizer(tokenizer);
    flatFileItemReader.setEncoding("UTF-8");
    flatFileItemReader.setLinesToSkip(0);
    flatFileItemReader.setResource(content);
    flatFileItemReader.setLineMapper(lineMapper);
    flatFileItemReader.afterPropertiesSet();
}
Also used : DelimitedLineTokenizer(org.springframework.batch.item.file.transform.DelimitedLineTokenizer) TaxonService(org.powo.api.TaxonService) FieldSetMapper(org.powo.job.dwc.taxon.FieldSetMapper) Taxon(org.powo.model.Taxon) NomenclaturalStatusConverter(org.powo.model.convert.NomenclaturalStatusConverter) DefaultLineMapper(org.springframework.batch.item.file.mapping.DefaultLineMapper) RankConverter(org.powo.model.convert.RankConverter) ConversionServiceFactoryBean(org.springframework.context.support.ConversionServiceFactoryBean) ConversionService(org.springframework.core.convert.ConversionService) StringToIsoDateTimeConverter(org.powo.model.convert.StringToIsoDateTimeConverter) Converter(org.springframework.core.convert.converter.Converter) TaxonomicStatusConverter(org.powo.model.convert.TaxonomicStatusConverter) RankConverter(org.powo.model.convert.RankConverter) StringToIsoDateTimeConverter(org.powo.model.convert.StringToIsoDateTimeConverter) NomenclaturalStatusConverter(org.powo.model.convert.NomenclaturalStatusConverter) TaxonomicStatusConverter(org.powo.model.convert.TaxonomicStatusConverter) HashSet(java.util.HashSet) Before(org.junit.Before)

Aggregations

DefaultLineMapper (org.springframework.batch.item.file.mapping.DefaultLineMapper)11 DelimitedLineTokenizer (org.springframework.batch.item.file.transform.DelimitedLineTokenizer)11 Bean (org.springframework.context.annotation.Bean)5 FlatFileItemReader (org.springframework.batch.item.file.FlatFileItemReader)4 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 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 Converter (org.springframework.core.convert.converter.Converter)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 User (com.ask.springbatch.entity.User)1 Person (com.gxitsky.entity.Person)1 Employee (com.pkslow.batch.entity.Employee)1 Row (org.avni.importer.batch.model.Row)1