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;
}
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;
}
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;
}
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();
}
}
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;
}
Aggregations