您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“Spring Batch如何實現輕量級批處理框架”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Spring Batch如何實現輕量級批處理框架”這篇文章吧。
實踐內容
從 MariaDB 一張表內讀 10 萬條記錄,經處理后寫到 MongoDB 。
具體實現
1、新建 Spring Boot 應用,依賴如下:
<!-- Web 應用 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- Web 容器 undertow --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency> <!-- 日志 Log4j2 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <!-- MongoDB --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <!-- Brantch --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId> </dependency> <!-- Mariadb 驅動 --> <dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <version>2.0.2</version> </dependency> <!-- Lombok 代碼簡化 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.14</version> </dependency>
2、創建一張表,并生成 10 萬條數據
DROP TABLE people IF EXISTS; CREATE TABLE people ( id BIGINT IDENTITY NOT NULL PRIMARY KEY, first_name VARCHAR(20), last_name VARCHAR(20) );
3、創建 Person 類
@Data public class Person { private Long id; private String lastName; private String firstName; }
4、創建一個中間處理器 PersonItemProcessor
import org.springframework.batch.item.ItemProcessor; @Log4j2 public class PersonItemProcessor implements ItemProcessor<Person, Person> { @Override public Person process(final Person person) throws Exception { final String firstName = person.getFirstName().toUpperCase(); final String lastName = person.getLastName().toUpperCase(); final Person transformedPerson = new Person(firstName, lastName); log.info("Converting (" + person + ") into (" + transformedPerson + ")"); return transformedPerson; } }
5、創建 PersonMapper
,用戶數據庫映射
public class PersonMapper implements RowMapper { private static final String ID_COLUMN = "id"; private static final String NICKNAME_COLUMN = "first_name"; private static final String EMAIL_COLUMN = "last_name"; @Override public Object mapRow(ResultSet resultSet, int i) throws SQLException { Person user = new Person(); person.setId(resultSet.getLong(ID_COLUMN)); person.setNickname(resultSet.getString(NICKNAME_COLUMN)); person.setEmail(resultSet.getString(EMAIL_COLUMN)); return person; } }
6、創建任務完成的監聽 JobCompletionNotificationListener
@Log4j2 @Component public class JobCompletionNotificationListener extends JobExecutionListenerSupport { @Override public void afterJob(JobExecution jobExecution) { if(jobExecution.getStatus() == BatchStatus.COMPLETED) { log.info("!!! JOB FINISHED! Time to verify the results"); } } }
7、構建批處理任務 BatchConfiguration
@Configuration @EnableBatchProcessing public class BatchConfiguration { @Autowired public JobBuilderFactory jobBuilderFactory; @Autowired public StepBuilderFactory stepBuilderFactory; @Autowired public DataSource dataSource; @Autowired public MongoTemplate mongoTemplate; @Bean public JdbcCursorItemReader<Person> reader(){ JdbcCursorItemReader<Person> itemReader = new JdbcCursorItemReader<Person>(); itemReader.setDataSource(dataSource); itemReader.setSql("select id, nickname, email from people"); itemReader.setRowMapper(new PersonMapper()); return itemReader; } @Bean public PersonItemProcessor processor() { return new PersonItemProcessor(); } @Bean MongoItemWriter<Person> writer(){ MongoItemWriter<Person> itemWriter = new MongoItemWriter<Person>(); itemWriter.setTemplate(mongoTemplate); itemWriter.setCollection("branch"); return itemWriter; } @Bean public Step step() { return stepBuilderFactory.get("step") .<Person, Person> chunk(10) .reader(reader()) .processor(processor()) .writer(writer()) .build(); } @Bean public Job importUserJob(JobCompletionNotificationListener listener) { return jobBuilderFactory.get("importUserJob") .incrementer(new RunIdIncrementer()) .listener(listener) .flow(step()) .end() .build(); } }
任務處理結果
0出錯,耗時 2 分鐘左右,測試機 Mac
以上是“Spring Batch如何實現輕量級批處理框架”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。