【问题标题】:Spring Batch reading file with related recordsSpring Batch 读取文件及相关记录
【发布时间】:2013-03-07 08:54:07
【问题描述】:

我正在使用 Spring Batch 读取平面文件。该文件有相关记录。即,可以有一个父记录和任意数量的子记录,我想读取所有记录并调用 Web 服务来存储它。我还想捕获关系并将其存储。一个挑战是子记录可以在文件中的任何位置。而且孩子也可以有很多孩子的记录。我无法用spring batch找到这个问题的解决方案。 请提供您的建议

更新:我没有任何选项可以使用数据库作为数据的临时存储。

【问题讨论】:

    标签: spring batch-processing spring-batch


    【解决方案1】:

    我已经通过多次处理文件解决了这个问题。

    每次通过时,我都会尝试使用这样的算法读取\处理文件中的每条记录:

    • 如果记录有父项 - 检查父项是否已存储。如果没有 - 我在处理器中跳过它
    • 如果记录未更改(或者如果无法更新,则已存储)- 在处理器中跳过它
    • else - 存储在数据库中

    然后声明循环和判定器:

        <batch:step id="processParentAndChilds" next="loop">
            <batch:tasklet>
                <batch:chunk reader="processParentAndChildsReader"
                             commit-interval="1000">
                    <batch:processor>
                        <bean class="processParentAndChildsProcessor"/>
                    </batch:processor>
                    <batch:writer>
                        <bean class="processParentAndChildsWriter"/>
                    </batch:writer>
                </batch:chunk>
            </batch:tasklet>
        </batch:step>
        <batch:decision decider="processParentAndChildsRetryDecider" id="loop">
            <batch:next on="NEXT_LEVEL" to="processprocessParentAndChilds"/>
            <batch:next on="COMPLETED" to="goToNextSteps"/>
        </batch:decision> 
    
    
    public class ProcessParentAndChildsRetryDecider implements JobExecutionDecider{
    @Override
    public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
        // if no on record written - no sense to try again
        if (stepExecution.getWriteCount() > 0) {
            return new FlowExecutionStatus("NEXT_LEVEL");
        } else {
            return FlowExecutionStatus.COMPLETED;
        }
    }
    

    }

    【讨论】:

    • 谢谢迈克尔。我使用了另一种方法。我有一个步骤,我遍历文件并创建内存映射(读取器读取文件,处理器创建映射。写入器是无操作写入器)。我保存了这个上下文,在下一步中我通读了地图并创建了域对象和所需的关系。我知道如果文件很大,它会造成内存泄漏。我可以为这个处理购买更多内存并解决内存问题。因为我需要避免多次处理文件。还是谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    相关资源
    最近更新 更多