SpringBatch介绍:
创新互联服务项目包括雁塔网站建设、雁塔网站制作、雁塔网页制作以及雁塔网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,雁塔网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到雁塔省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
SpringBatch 是一个大数据量的并行处理框架。通常用于数据的离线迁移,和数据处理,⽀持事务、并发、流程、监控、纵向和横向扩展,提供统⼀的接⼝管理和任务管理;SpringBatch是SpringSource和埃森哲为了统一业界并行处理标准为广大开发者提供方便开发的一套框架。
官方地址:github.com/spring-projects/spring-batch
快速入门
pom.xml 添加
org.springframework.boot spring-boot-starter-batch
创建BatchConfig(可以是其他类名)
@Configuration @EnableBatchProcessing public class BatchConfig { // tag::readerwriterprocessor[] @Bean public FlatFileItemReaderflatFileItemReader() { FlatFileItemReader reader = new FlatFileItemReader<>(); reader.setResource(new ClassPathResource("sample-data.csv")); FixedLengthTokenizer fixedLengthTokenizer = new FixedLengthTokenizer(); reader.setLineMapper(new DefaultLineMapper () {{ setLineTokenizer(new DelimitedLineTokenizer() {{ setNames(new String[]{"firstName", "lastName"}); }}); setFieldSetMapper(new BeanWrapperFieldSetMapper () {{ setTargetType(Person.class); }}); }}); return reader; } @Bean public JdbcPagingItemReader jdbcPagingItemReader(DataSource dataSource) { JdbcPagingItemReader reader = new JdbcPagingItemReader<>(); reader.setDataSource(dataSource); reader.setFetchSize(100); reader.setQueryProvider(new MySQLPagingQueryProvider() {{ setSelectClause("SELECT person_id,first_name,last_name"); setFromClause("from people"); setWhereClause("last_name=:lastName"); setSortKeys(new HashMap () {{ put("person_id", Order.ASCENDING); }}); }}); reader.setParameterValues(new HashMap () {{ put("lastName", "DOE"); }}); reader.setRowMapper(new BeanPropertyRowMapper<>(Person.class)); return reader; } @Bean public JdbcBatchItemWriter jdbcBatchItemWriter(DataSource dataSource) { JdbcBatchItemWriter writer = new JdbcBatchItemWriter<>(); writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>()); writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)"); writer.setDataSource(dataSource); return writer; } /*@Bean public FlatFileItemWriter flatFileItemWriter(DataSource dataSource) { FlatFileItemWriter writer = new FlatFileItemWriter<>(); writer.setAppendAllowed(true); writer.setEncoding("UTF-8"); // writer.set(dataSource); return writer; }*/ // end::readerwriterprocessor[] // tag::jobstep[] @Bean public Job importUserJob(JobBuilderFactory jobBuilderFactory, JobCompletionNotificationListener listener, Step step) { return jobBuilderFactory.get("importUserJob") .incrementer(new RunIdIncrementer()) .listener(listener) .start(step) .build(); } @Bean public Step step1(StepBuilderFactory stepBuilderFactory, PersonItemProcessor processor, ItemWriter jdbcBatchItemWriter, ItemReader flatFileItemReader) { /*CompositeItemProcessor compositeItemProcessor = new CompositeItemProcessor(); compositeItemProcessor.setDelegates(Lists.newArrayList(processor, processor));*/ return stepBuilderFactory.get("step1") . chunk(10) .reader(flatFileItemReader) .processor(processor) .writer(jdbcBatchItemWriter) .build(); } // end::jobstep[] }
Spring Batch的分层架构
Spring Batch执行流程
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对创新互联的支持。