欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Spring Batch 輕量級批處理框架實(shí)踐

 更新時間:2019年06月27日 14:53:43   作者:Anoy  
這篇文章主要介紹了詳解Spring Batch 輕量級批處理框架實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

實(shí)踐內(nèi)容

從 MariaDB 一張表內(nèi)讀 10 萬條記錄,經(jīng)處理后寫到 MongoDB 。

具體實(shí)現(xiàn)

1、新建 Spring Boot 應(yīng)用,依賴如下:

    <!-- Web 應(yīng)用 -->
    <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 驅(qū)動 -->
    <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、創(chuàng)建一張表,并生成 10 萬條數(shù)據(jù)

DROP TABLE people IF EXISTS;

CREATE TABLE people (
  id BIGINT IDENTITY NOT NULL PRIMARY KEY,
  first_name VARCHAR(20),
  last_name VARCHAR(20)
);

3、創(chuàng)建 Person 類

@Data
public class Person {
  private Long id;
  private String lastName;
  private String firstName;
}

4、創(chuàng)建一個中間處理器 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、創(chuàng)建 PersonMapper,用戶數(shù)據(jù)庫映射

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、創(chuàng)建任務(wù)完成的監(jiān)聽 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、構(gòu)建批處理任務(wù) 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();
  }

}

任務(wù)處理結(jié)果

0出錯,耗時 2 分鐘左右,測試機(jī) Mac

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 兩種JAVA實(shí)現(xiàn)短網(wǎng)址服務(wù)算法

    兩種JAVA實(shí)現(xiàn)短網(wǎng)址服務(wù)算法

    這篇文章介紹了兩種JAVA實(shí)現(xiàn)短網(wǎng)址服務(wù)算法,一種是基于MD5碼的,一種是基于自增序列的,需要的朋友可以參考下
    2015-07-07
  • SpringBoot項(xiàng)目修改訪問端口和訪問路徑的方法

    SpringBoot項(xiàng)目修改訪問端口和訪問路徑的方法

    這篇文章主要介紹了SpringBoot項(xiàng)目修改訪問端口和訪問路徑的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • Java泛型extends關(guān)鍵字設(shè)置邊界的實(shí)現(xiàn)

    Java泛型extends關(guān)鍵字設(shè)置邊界的實(shí)現(xiàn)

    這篇文章主要介紹了Java泛型extends關(guān)鍵字設(shè)置邊界的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Java spring AOP基礎(chǔ)

    Java spring AOP基礎(chǔ)

    本篇文章主要介紹了深入理解spring的AOP機(jī)制基礎(chǔ)原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-11-11
  • Mybatis新手教程之簡單入門

    Mybatis新手教程之簡單入門

    這篇文章主要給大家介紹了關(guān)于Mybatis新手教程之簡單入門的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • Java中Easypoi實(shí)現(xiàn)excel多sheet表導(dǎo)入導(dǎo)出功能

    Java中Easypoi實(shí)現(xiàn)excel多sheet表導(dǎo)入導(dǎo)出功能

    這篇文章主要介紹了Java中Easypoi實(shí)現(xiàn)excel多sheet表導(dǎo)入導(dǎo)出功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • 一文搞懂Mybatis中Mapper配置文件獲取參數(shù)的五種方式

    一文搞懂Mybatis中Mapper配置文件獲取參數(shù)的五種方式

    這篇文章主要介紹了Mybatis中Mapper配置文件獲取參數(shù)的五種方式,文中通過代碼示例講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-03-03
  • Java二進(jìn)制操作(動力節(jié)點(diǎn)Java學(xué)院整理)

    Java二進(jìn)制操作(動力節(jié)點(diǎn)Java學(xué)院整理)

    這篇文章給大家介紹了java二進(jìn)制操作技巧,包括移位、位運(yùn)算操作符等相關(guān)知識點(diǎn),非常不錯,感興趣的朋友參考下吧
    2017-03-03
  • 一篇文章帶你深入了解Java對象與Java類

    一篇文章帶你深入了解Java對象與Java類

    這篇文章主要給大家介紹了關(guān)于java中類和對象的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • 一篇文章帶你了解JAVA面對對象之繼承與修飾符

    一篇文章帶你了解JAVA面對對象之繼承與修飾符

    這篇文章主要介紹了Java面向?qū)ο缶幊讨惖睦^承,結(jié)合實(shí)例形式較為詳細(xì)的分析了Java面向?qū)ο缶幊填惖母拍?、功能、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2021-08-08

最新評論