简体   繁体   English

Spring Batch JdbcPagingItemReader似乎没有执行所有项目

[英]Spring Batch JdbcPagingItemReader seems not EXECUTING ALL THE ITEMS

I'm working on an app that extract records from an Oracle database and then are exported as one single tabulated file. 我正在开发一个从Oracle数据库提取记录,然后将其导出为一个列表文件的应用程序。

However, when I attempt to read from the DB using JdbcPagingItemReader and write to a file I only get the number of records specified in pageSize. 但是,当我尝试使用JdbcPagingItemReader从数据库读取并写入文件时,我仅获得pageSize中指定的记录数。 So if the pageSize is 10, then I get a file with 10 lines and the rest of the records seem to be ignored. 因此,如果pageSize为10,那么我得到一个包含10行的文件,其余的记录似乎被忽略了。 So far, I haven't been able to find whats is really going on and any help would be most welcome. 到目前为止,我还没有找到真正发生的事情,任何帮助都将受到欢迎。

Here is the JdbcPagingItemReader config: 这是JdbcPagingItemReader配置:

<bean id="databaseItemReader"
class="org.springframework.batch.item.database.JdbcPagingItemReader" >
    <property name="dataSource" ref="dataSourceTest" />
<property name="queryProvider">
  <bean
    class="org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean">
    <property name="dataSource" ref="dataSourceTest" />
    <property name="selectClause" value="SELECT *" />
    <property name="fromClause" value="FROM *****" />
    <property name="whereClause" value="where snapshot_id=:name" />
    <property name="sortKey" value="snapshot_id" />
  </bean>
</property>
<property name="parameterValues">
   <map>
    <entry key="name" value="18596" />
   </map>
</property>
    <property name="pageSize" value="100000" />
<property name="rowMapper">
    <bean class="com.mkyong.ViewRowMapper" />
</property>

<bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
<!-- write to this csv file -->
<property name="resource" value="file:cvs/report.csv" />
<property name="shouldDeleteIfExists" value="true" />

<property name="lineAggregator">
      <bean
       class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
    <property name="delimiter" value=";" />
    <property name="fieldExtractor">
          <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
        <property name="names" value="ID" />
       </bean>
    </property>
       </bean>  
</property>

  <job id="testJob" xmlns="http://www.springframework.org/schema/batch">
<step id="step1">
  <tasklet>
    <chunk reader="databaseItemReader" writer="itemWriter" commit-interval="1" />
  </tasklet>
</step>

thanks 谢谢

it was the scope="step" that was missing it should be: 缺少的是scope =“ step”,应该为:

<bean id="databaseItemReader"
    class="org.springframework.batch.item.database.JdbcPagingItemReader" scope="step">

Your setting seem incorrect for whereClause and sort key can not be same because pagesize works hand to hand with your sorting column name. 您的设置似乎不正确,因为whereClause和sort键不能相同,因为pagesize与您的排序列名称配合使用。

Check how is your data(in corresponding table) looks like. 检查您的数据(在相应表中)的样子。

In spring batch , as per your configuration, spring will create and execute as given below.. first query executed with pagesize = 10 , is like following 在spring batch中,按照您的配置,spring将按照以下给定的方式创建和执行..第一个以pagesize = 10执行的查询如下所示

SELECT top 10  FROM tableName where snapshot_id=18596 snapshot_id > 10

Second /remaining query executed depends on your sort key. 执行的第二个/剩余查询取决于您的排序键。

SELECT *  FROM tableName  where snapshot_id=18596 snapshot_id > 10
SELECT *  FROM tableName  where snapshot_id=18596 snapshot_id > 20

and so on.. try running this query in database , doesn't it look weird . 依此类推..尝试在数据库中运行这个查询,看起来并不奇怪。 :-) :-)

If you don't need where clause, remove it. 如果不需要where子句,请将其删除。

And if possible keep page size and commit-interval same, because that's how you decide to process and persist. 并尽可能保持页面大小和commit-interval相同,因为这是您决定处理和持久化的方式。 But of course that depends on your design. 但是,当然这取决于您的设计。 So you decide. 所以你决定。

Adding @StepScope made my item reader take off with paging capability. 添加@StepScope使我的项目阅读器具有分页功能。

@Bean
@StepScope
ItemReader<Account> ItemReader(@Value("#{jobParameters[id]}") String id) {
    JdbcPagingItemReader<Account> databaseReader = new JdbcPagingItemReader<>();
    databaseReader.setDataSource(dataSource);
    databaseReader.setPageSize(100);
    databaseReader.setFetchSize(100);
    PagingQueryProvider queryProvider = createQueryProvider(id);
    databaseReader.setQueryProvider(queryProvider);
    databaseReader.setRowMapper(new BeanPropertyRowMapper<>(Account.class));
    return databaseReader;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM