SpringBatch介紹:
創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括雁塔網(wǎng)站建設(shè)、雁塔網(wǎng)站制作、雁塔網(wǎng)頁制作以及雁塔網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,雁塔網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到雁塔省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
SpringBatch 是一個(gè)大數(shù)據(jù)量的并行處理框架。通常用于數(shù)據(jù)的離線遷移,和數(shù)據(jù)處理,⽀持事務(wù)、并發(fā)、流程、監(jiān)控、縱向和橫向擴(kuò)展,提供統(tǒng)⼀的接⼝管理和任務(wù)管理;SpringBatch是SpringSource和埃森哲為了統(tǒng)一業(yè)界并行處理標(biāo)準(zhǔn)為廣大開發(fā)者提供方便開發(fā)的一套框架。
官方地址:github.com/spring-projects/spring-batch
快速入門
pom.xml 添加
org.springframework.boot spring-boot-starter-batch
創(chuàng)建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的分層架構(gòu)
Spring Batch執(zhí)行流程
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)創(chuàng)新互聯(lián)的支持。