10 Aralık 2018 Pazartesi

SpringBatch JdbcPagingItemReader Sınıfı

Giriş
RepositoryItemReader sınıfı ile kardeştir. Açıklaması şöyle.
These readers will read a page once , keep it in memory , and process single - single items till chunk size is reached & then commit happens. DB read wouldn't happen till one page is finished. Generally, for optimal performance , chunk size needs to be few times smaller than page size e.g. reader page size - 1000 & chunk size = 100 so 1000 items would be read once and committed in chunks of 100 - 100 items.
setDataSource metodu
Şöyle yaparız.
@Configuration
public class JobConfiguration {
 
 
  @Autowired
  private DataSource dataSource;

  @Bean
  public JdbcPagingItemReader<Customer> customerPagingItemReader(){
   // reading database records using JDBC in a paging fashion
   JdbcPagingItemReader<Customer> reader = new JdbcPagingItemReader<>();
   reader.setDataSource(this.dataSource);
   reader.setFetchSize(1000);
   reader.setRowMapper(new CustomerRowMapper());

   // Sort Keys
   Map<String, Order> sortKeys = new HashMap<>();
   sortKeys.put("id", Order.ASCENDING);

   // MySQL implementation of a PagingQueryProvider using database specific features.
    MySqlPagingQueryProvider queryProvider = new MySqlPagingQueryProvider();
    queryProvider.setSelectClause("id, firstName, lastName, birthdate");
    queryProvider.setFromClause("from customer");
    queryProvider.setSortKeys(sortKeys);

    reader.setQueryProvider(queryProvider);

    return reader;
  }
  ...

}

Hiç yorum yok:

Yorum Gönder