Search in sources :

Example 1 with DelimitedLineTokenizer

use of org.springframework.batch.item.file.transform.DelimitedLineTokenizer in project microservices by pwillhan.

the class ContactBatchJobConfiguration method reader.

@Bean
public ItemReader<Contact> reader() {
    FlatFileItemReader<Contact> reader = new FlatFileItemReader<>();
    reader.setResource(new ClassPathResource("data.csv"));
    reader.setLineMapper(new DefaultLineMapper<Contact>() {

        {
            setLineTokenizer(new DelimitedLineTokenizer() {

                {
                    setNames(new String[] { "firstName", "lastName", "email" });
                }
            });
            setFieldSetMapper(new BeanWrapperFieldSetMapper<Contact>() {

                {
                    setTargetType(Contact.class);
                }
            });
        }
    });
    return reader;
}
Also used : FlatFileItemReader(org.springframework.batch.item.file.FlatFileItemReader) DelimitedLineTokenizer(org.springframework.batch.item.file.transform.DelimitedLineTokenizer) ClassPathResource(org.springframework.core.io.ClassPathResource) BeanWrapperFieldSetMapper(org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper) Bean(org.springframework.context.annotation.Bean)

Example 2 with DelimitedLineTokenizer

use of org.springframework.batch.item.file.transform.DelimitedLineTokenizer 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 3 with DelimitedLineTokenizer

use of org.springframework.batch.item.file.transform.DelimitedLineTokenizer 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 4 with DelimitedLineTokenizer

use of org.springframework.batch.item.file.transform.DelimitedLineTokenizer in project microservices by pwillhan.

the class BatchConfiguration method flatFileItemReader.

@Bean
@StepScope
FlatFileItemReader<Person> flatFileItemReader(@Value("#{jobParameters[file]}") File file) {
    FlatFileItemReader<Person> r = new FlatFileItemReader<>();
    r.setResource(new FileSystemResource(file));
    r.setLineMapper(new DefaultLineMapper<Person>() {

        {
            this.setLineTokenizer(new DelimitedLineTokenizer(",") {

                {
                    this.setNames(new String[] { "first", "last", "email" });
                }
            });
            this.setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {

                {
                    this.setTargetType(Person.class);
                }
            });
        }
    });
    return r;
}
Also used : FlatFileItemReader(org.springframework.batch.item.file.FlatFileItemReader) DelimitedLineTokenizer(org.springframework.batch.item.file.transform.DelimitedLineTokenizer) FileSystemResource(org.springframework.core.io.FileSystemResource) BeanWrapperFieldSetMapper(org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper) StepScope(org.springframework.batch.core.configuration.annotation.StepScope) Bean(org.springframework.context.annotation.Bean)

Aggregations

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