Search in sources :

Example 1 with FlatFileItemReader

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

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

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

use of org.springframework.batch.item.file.FlatFileItemReader in project Spring-Family by Sierou-Java.

the class Main method main.

public static void main(String[] args) {
    try {
        String[] configLocations = { "classpath*:applicationContext.xml" };
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(configLocations);
        JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class);
        JobRepository jobRepository = applicationContext.getBean(JobRepository.class);
        PlatformTransactionManager transactionManager = applicationContext.getBean(PlatformTransactionManager.class);
        // 创建reader
        FlatFileItemReader<DeviceCommand> flatFileItemReader = new FlatFileItemReader<>();
        flatFileItemReader.setResource(new FileSystemResource("/project/Spring-Family-Parent/Spring-Family-Batch/src/main/resources/batch-data.csv"));
        // // 转换数据
        flatFileItemReader.setLineMapper(new DemoLineMapper());
        // 创建processor 对数据进行操作
        DemoProcessor helloItemProcessor = new DemoProcessor();
        // 创建writer
        FlatFileItemWriter<DeviceCommand> flatFileItemWriter = new FlatFileItemWriter<>();
        flatFileItemWriter.setResource(new FileSystemResource("/project/Spring-Family-Parent/Spring-Family-Batch/src/main/resources/batch-data.csv"));
        flatFileItemWriter.setLineAggregator(new DemoLineAggregator());
        // 创建Step
        StepBuilderFactory stepBuilderFactory = new StepBuilderFactory(jobRepository, transactionManager);
        Step step = stepBuilderFactory.get("step").<DeviceCommand, DeviceCommand>chunk(1).reader(// 读操作
        flatFileItemReader).processor(// 处理操作
        helloItemProcessor).writer(// 写操作
        flatFileItemWriter).build();
        // 创建Job
        JobBuilderFactory jobBuilderFactory = new JobBuilderFactory(jobRepository);
        Job job = jobBuilderFactory.get("job").start(step).build();
        // 启动任务
        jobLauncher.run(job, new JobParameters());
    } catch (JobExecutionAlreadyRunningException e) {
        e.printStackTrace();
    } catch (JobRestartException e) {
        e.printStackTrace();
    } catch (JobInstanceAlreadyCompleteException e) {
        e.printStackTrace();
    } catch (JobParametersInvalidException e) {
        e.printStackTrace();
    }
}
Also used : JobLauncher(org.springframework.batch.core.launch.JobLauncher) FlatFileItemReader(org.springframework.batch.item.file.FlatFileItemReader) DemoProcessor(org.family.processor.DemoProcessor) JobExecutionAlreadyRunningException(org.springframework.batch.core.repository.JobExecutionAlreadyRunningException) FileSystemResource(org.springframework.core.io.FileSystemResource) Step(org.springframework.batch.core.Step) JobRepository(org.springframework.batch.core.repository.JobRepository) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) JobParametersInvalidException(org.springframework.batch.core.JobParametersInvalidException) Job(org.springframework.batch.core.Job) DemoLineAggregator(org.family.aggregator.DemoLineAggregator) StepBuilderFactory(org.springframework.batch.core.configuration.annotation.StepBuilderFactory) DeviceCommand(org.family.dto.DeviceCommand) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) JobInstanceAlreadyCompleteException(org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException) JobBuilderFactory(org.springframework.batch.core.configuration.annotation.JobBuilderFactory) DemoLineMapper(org.family.mapper.DemoLineMapper) FlatFileItemWriter(org.springframework.batch.item.file.FlatFileItemWriter) JobParameters(org.springframework.batch.core.JobParameters) JobRestartException(org.springframework.batch.core.repository.JobRestartException)

Example 5 with FlatFileItemReader

use of org.springframework.batch.item.file.FlatFileItemReader 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)5 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 FileSystemResource (org.springframework.core.io.FileSystemResource)2 DemoLineAggregator (org.family.aggregator.DemoLineAggregator)1 DeviceCommand (org.family.dto.DeviceCommand)1 DemoLineMapper (org.family.mapper.DemoLineMapper)1 DemoProcessor (org.family.processor.DemoProcessor)1 Job (org.springframework.batch.core.Job)1 JobParameters (org.springframework.batch.core.JobParameters)1 JobParametersInvalidException (org.springframework.batch.core.JobParametersInvalidException)1 Step (org.springframework.batch.core.Step)1 JobBuilderFactory (org.springframework.batch.core.configuration.annotation.JobBuilderFactory)1 StepBuilderFactory (org.springframework.batch.core.configuration.annotation.StepBuilderFactory)1