Java Code Examples for org.springframework.batch.item.database.JdbcPagingItemReader

The following examples show how to use org.springframework.batch.item.database.JdbcPagingItemReader. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: CogStack-Pipeline   Source File: JobConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
@StepScope
@Qualifier("documentItemReader")
@Profile("jdbc_in")
public ItemReader<Document> documentItemReader(
        @Value("#{stepExecutionContext[minValue]}") String minValue,
        @Value("#{stepExecutionContext[maxValue]}") String maxValue,
        @Value("#{stepExecutionContext[min_time_stamp]}") String minTimeStamp,
        @Value("#{stepExecutionContext[max_time_stamp]}") String maxTimeStamp,
        @Qualifier("documentRowMapper")RowMapper<Document> documentRowmapper,
        @Qualifier("sourceDataSource") DataSource jdbcDocumentSource) throws Exception {

    JdbcPagingItemReader<Document> reader = new JdbcPagingItemReader<>();
    reader.setDataSource(jdbcDocumentSource);

    // read and set obligatory properties
    SqlPagingQueryProviderFactoryBean qp = new SqlPagingQueryProviderFactoryBean();
    qp.setSelectClause(env.getRequiredProperty("source.selectClause"));
    qp.setFromClause(env.getRequiredProperty("source.fromClause"));
    qp.setSortKey(env.getRequiredProperty("source.sortKey"));
    qp.setWhereClause(stepPartitioner.getPartitioningLogic(minValue,maxValue, minTimeStamp,maxTimeStamp));
    qp.setDataSource(jdbcDocumentSource);

    // set optional properties
    if (env.containsProperty("source.pageSize")) {
        reader.setPageSize(Integer.parseInt(env.getProperty("source.pageSize")));
    }
    else { // it's a good idea to batch size and page size (commit interval) set to the same value
        LOG.info("property: 'source.pageSize' not specified -> setting DB reader page size to batch step chunk size: {}", chunkSize);
        reader.setPageSize(chunkSize);
    }
    reader.setQueryProvider(qp.getObject());
    reader.setRowMapper(documentRowmapper);
    return reader;
}
 
Example 2
Source Project: SpringAll   Source File: DataSourceItemReaderDemo.java    License: MIT License 4 votes vote down vote up
private ItemReader<TestData> dataSourceItemReader() throws Exception {
    JdbcPagingItemReader<TestData> reader = new JdbcPagingItemReader<>();
    reader.setDataSource(dataSource); // 设置数据源
    reader.setFetchSize(5); // 每次取多少条记录
    reader.setPageSize(5); // 设置每页数据量

    // 指定sql查询语句 select id,field1,field2,field3 from TEST
    MySqlPagingQueryProvider provider = new MySqlPagingQueryProvider();
    provider.setSelectClause("id,field1,field2,field3"); //设置查询字段
    provider.setFromClause("from TEST"); // 设置从哪张表查询

    // 将读取到的数据转换为TestData对象
    reader.setRowMapper((resultSet, rowNum) -> {
        TestData data = new TestData();
        data.setId(resultSet.getInt(1));
        data.setField1(resultSet.getString(2)); // 读取第一个字段,类型为String
        data.setField2(resultSet.getString(3));
        data.setField3(resultSet.getString(4));
        return data;
    });

    Map<String, Order> sort = new HashMap<>(1);
    sort.put("id", Order.ASCENDING);
    provider.setSortKeys(sort); // 设置排序,通过id 升序

    reader.setQueryProvider(provider);

    // 设置namedParameterJdbcTemplate等属性
    reader.afterPropertiesSet();
    return reader;
}