首页 > 解决方案 > jdbcItemReader 支持分区吗?

问题描述

使用https://github.com/jberet/jberet-support中的 jdbcItemReader并希望使用分区来加快处理速度。

分区数(此处为 16)对重复写入的数据有副作用。每个分区对相同的数据执行相同的工作,而不是将输入数据集拆分为 n 个不同的分区。

已编辑:下面的代码显示了实现它的正确方法。注意:您的 SQL 查询需要返回有序数据:N 个分区,这里是 16,表示 16 个 reader 将运行查询!

 <reader ref="jdbcItemReader">
                <properties>
                    <property name="beanType" value="java.util.Map"/>
                    <property name="sql" value="select ......"/>
                    <property name="url"
                              value="jdbc:oracle:thin:......"/>
                    <property name="user" value="......."/>
                    <property name="password" value="....."/>
                    <property name="columnMapping" value="xxxx, xxxx"/>
                    <property name="columnTypes" value="String,String"/>
                    <property name="start" value="#{partitionPlan['partition.start']}"/>
                    <property name="end" value="#{partitionPlan['partition.end']}"/>
                    <!--CONCUR_READ_ONLY: If you set this as a value of the concurrency while creating the ResultSet object you cannot update the contents of the ResultSet you can only read/retrieve them.-->
                    <!--CONCUR_UPDATABLE: If you set this as a value of the concurrency while creating the ResultSet object you can update the contents of the ResultSet.-->
                    <!--TYPE_SCROLL_SENSITIVE: ResultSet is sensitive to the changes that are made in the database i.e. the modifications done in the database are reflected in the ResultSet.-->
                    <property name="resultSetProperties"
                              value="fetchSize=5500, resultSetConcurrency=CONCUR_READ_ONLY,
                              fetchDirection=FETCH_REVERSE,
                              resultSetType=TYPE_SCROLL_SENSITIVE,
                              resultSetHoldability=HOLD_CURSORS_OVER_COMMIT"/>
                </properties>
            </reader>
            <processor ref="myProcessort"/>
            <writer ref="myWriter"/>
        </chunk>

<!-- run your sql with a count to define partitions evenly -->
        <partition>
            <plan partitions="16" threads="16">
                <properties partition="0">
                    <property name="partition.start" value="0"/>
                    <property name="partition.end" value="500"/>
                </properties>
                <properties partition="1">
                    <property name="partition.start" value="500"/>
                    <property name="partition.end" value="1000"/>
                </properties>
       <!-- ... -->
                <properties partition="15">
                    <property name="partition.start" value="5000"/>
                    <property name="partition.end" value="5500"/>
                </properties>

标签: javabatch-processingjsr352jberet

解决方案


您需要像在第二个 XML 片段中那样定义步骤分区。然后在你的 中定义startend属性jdbcItemReader,这两个属性分别引用了分区属性partition.startpartition.end

2 个分区属性可以用不同的方式命名,只要它们在partitionitem-reader元素中是一致的。

例如,

<reader ref="jdbcItemReader">
  <properties>
    <property name="start" value="#{partitionPlan['partition.start']}"/>
    <property name="end" value="#{partitionPlan['partition.end']}"/>
</properties>
</reader>

推荐阅读