use of org.springframework.batch.item.file.FlatFileItemWriter 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.FlatFileItemWriter in project nixmash-blog by mintster.
the class DemoJobConfiguration method demoJobWriter.
@Bean
public ItemWriter<PostDTO> demoJobWriter() {
FlatFileItemWriter<PostDTO> writer = new FlatFileItemWriter<>();
writer.setResource(new FileSystemResource("/home/daveburke/web/nixmashspring/posts.csv"));
DelimitedLineAggregator<PostDTO> delLineAgg = new DelimitedLineAggregator<>();
delLineAgg.setDelimiter(";");
BeanWrapperFieldExtractor<PostDTO> fieldExtractor = new BeanWrapperFieldExtractor<>();
fieldExtractor.setNames(new String[] { "postTitle" });
delLineAgg.setFieldExtractor(fieldExtractor);
writer.setLineAggregator(delLineAgg);
return writer;
}
use of org.springframework.batch.item.file.FlatFileItemWriter in project RecordManager2 by moravianlibrary.
the class ExportRecordsJobConfig method exportRecordsWriter.
@Bean(name = "exportRecordsJob:exportRecordsWriter")
@StepScope
public FlatFileItemWriter<String> exportRecordsWriter(@Value("#{jobParameters[" + Constants.JOB_PARAM_OUT_FILE + "]}") String filename) throws Exception {
FlatFileItemWriter<String> fileWritter = new FlatFileItemWriter<String>();
fileWritter.setAppendAllowed(false);
fileWritter.setShouldDeleteIfExists(true);
fileWritter.setEncoding("UTF-8");
fileWritter.setLineAggregator(new PassThroughLineAggregator<String>());
fileWritter.setResource(new FileSystemResource(filename));
fileWritter.afterPropertiesSet();
return fileWritter;
}
Aggregations