2016-11-19 72 views
0

即使在遵循Victor Jabor blog非常全面的示例之后,我仍无法获得此工作。我在他描述和使用所有最新的依赖关系时遵循了他的配置。我,正如维克多试图从一个数据库读取并写入另一个数据库。我有这个工作没有分区,但需要分区来提高性能,因为我需要能够在5分钟内读取5到1000万行。Spring引导批分区JdbcCursorItemReader错误

下面似乎工作: 1)ColumnRangePartitioner 2)TaskExecutorPartitionHandler的生成的基础上,gridsize步骤任务的正确数目和从由ColumnRangePartitioner设置stepExecution生成线程 3)setPreparedStatementSetter的正确数目。

但是,当我运行该应用程序时,我从JdbcCursorItemReader得到的错误是不一致的,我不明白。作为最后的手段,我将不得不调试JdbcCursorItemReader。我希望在此之前得到一些帮助,并希望这将是一个配置问题。

ERROR: Caused by: java.sql.SQLException: Exhausted Resultset at oracle.jdbc.driver.OracleResultSetImpl.getInt(OracleResultSetImpl.java:901) ~[ojdbc6-11.2.0.2.0.jar:11.2.0.2.0] at org.springframework.jdbc.support.JdbcUtils.getResultSetValue(JdbcUtils.java:160) ~[spring-jdbc-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.jdbc.core.BeanPropertyRowMapper.getColumnValue(BeanPropertyRowMapper.java:370) ~[spring-jdbc-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.jdbc.core.BeanPropertyRowMapper.mapRow(BeanPropertyRowMapper.java:291) ~[spring-jdbc-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.batch.item.database.JdbcCursorItemReader.readCursor(JdbcCursorItemReader.java:139) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]

配置类:

@Configuration @EnableBatchProcessing public class BatchConfiguration { 

    @Bean 
    public ItemProcessor<Archive, Archive> processor(@Value("${etl.region}") String region) { 
     return new ArchiveProcessor(region); 
    } 

    @Bean 
    public ItemWriter<Archive> writer(@Qualifier(value = "postgres") DataSource dataSource) { 
     JdbcBatchItemWriter<Archive> writer = new JdbcBatchItemWriter<>(); 

     writer.setSql("insert into tdw_src.archive (id) " + 
       "values (:id)"); 
     writer.setDataSource(dataSource); 
     writer.setItemSqlParameterSourceProvider(new org.springframework.batch.item.database. 
       BeanPropertyItemSqlParameterSourceProvider<>()); 
     return writer; 
    } 

    @Bean 
    public Partitioner archivePartitioner(@Qualifier(value = "gmDataSource") DataSource dataSource, 
              @Value("ROWNUM") String column, 
              @Value("archive") String table, 
              @Value("${gm.datasource.username}") String schema) { 
     return new ColumnRangePartitioner(dataSource, column, schema + "." + table); 
    } 

    @Bean 
    public Job archiveJob(JobBuilderFactory jobs, Step partitionerStep, JobExecutionListener listener) { 
     return jobs.get("archiveJob") 
       .preventRestart() 
       .incrementer(new RunIdIncrementer()) 
       .listener(listener) 
       .start(partitionerStep) 
       .build(); 
    } 

    @Bean 
    public Step partitionerStep(StepBuilderFactory stepBuilderFactory, 
           Partitioner archivePartitioner, 
           Step step1, 
           @Value("${spring.batch.gridsize}") int gridSize) { 
     return stepBuilderFactory.get("partitionerStep") 
       .partitioner(step1) 
       .partitioner("step1", archivePartitioner) 
       .gridSize(gridSize) 
       .taskExecutor(taskExecutor()) 
       .build(); 
    } 

    @Bean(name = "step1") 
    public Step step1(StepBuilderFactory stepBuilderFactory, ItemReader<Archive> customReader, 
         ItemWriter<Archive> writer, ItemProcessor<Archive, Archive> processor) { 
     return stepBuilderFactory.get("step1") 
       .listener(customReader) 
       .<Archive, Archive>chunk(5) 
       .reader(customReader) 
       .processor(processor) 
       .writer(writer) 
       .build(); 
    } 

    @Bean 
    public TaskExecutor taskExecutor(){ 
     return new SimpleAsyncTaskExecutor(); 
    } 

    @Bean 
    public SimpleJobLauncher getJobLauncher(JobRepository jobRepository) { 
     SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); 
     jobLauncher.setJobRepository(jobRepository); 
     return jobLauncher; 
    } 

Custom Reader:- 

public class CustomReader extends JdbcCursorItemReader<Archive> implements StepExecutionListener { 

    private StepExecution stepExecution; 

    @Autowired 
    public CustomReader(@Qualifier(value = "gmDataSource") DataSource geomangerDataSource, 
         @Value("${gm.datasource.username}") String schema) throws Exception { 
     super(); 
     this.setSql("SELECT TMP.* FROM (SELECT ROWNUM AS ID_PAGINATION, id FROM " + schema + ".archive) TMP " + 
       "WHERE TMP.ID_PAGINATION >= ? AND TMP.ID_PAGINATION <= ?"); 
     this.setDataSource(geomangerDataSource); 
     BeanPropertyRowMapper<Archive> rowMapper = new BeanPropertyRowMapper<>(Archive.class); 
     this.setRowMapper(rowMapper); 
     this.setFetchSize(5); 
     this.setSaveState(false); 

     this.setVerifyCursorPosition(false); 
// not sure if this is needed?  this.afterPropertiesSet(); 
    } 

    @Override 
    public synchronized void beforeStep(StepExecution stepExecution) { 
     this.stepExecution = stepExecution; 
     this.setPreparedStatementSetter(getPreparedStatementSetter()); 
    } 

    private PreparedStatementSetter getPreparedStatementSetter() { 
     ListPreparedStatementSetter listPreparedStatementSetter = new ListPreparedStatementSetter(); 
     List<Integer> list = new ArrayList<>(); 
     list.add(stepExecution.getExecutionContext().getInt("minValue")); 
     list.add(stepExecution.getExecutionContext().getInt("maxValue")); 
     listPreparedStatementSetter.setParameters(list); 
     LOGGER.debug("getPreparedStatementSetter list: " + list); 
     return listPreparedStatementSetter; 
    } 

    @Override 
    public ExitStatus afterStep(StepExecution stepExecution) { 
     return null; 
    } 
} 
+0

移除顾客阅读器用作成分,并加入到批量配置如下:@Bean 公共ItemReader 读数器(@Qualifier(值= “gmDataSource”)的DataSource geomangerDataSource, @Value(“$ {geomanager.datasource.username} “)字符串模式)抛出异常{ return new CustomReader(geomangerDataSource,schema); } 仍然得到同样的错误: 产生的原因:java.sql.SQLException中:力竭结果集 \t在oracle.jdbc.driver.OracleResultSetImpl.getTimestamp(OracleResultSetImpl.java:1381)〜[ojdbc6-11.2.0.2。 0.jar:11.2.0.2.0] – user103122

回答

0

我已经得到了这一切工作。

首先,我需要在我的CustomReader中对我的select语句进行排序,因此rownum对于所有线程都保持不变,最后,我必须使用@StepScope来对步骤中使用的每个bean使用@StepScope范围。

在现实中,我不会使用rownum,因为这需要进行排序以减少松散的性能,因此我将使用pk列来获得最佳性能。