【问题标题】:Spring Batch to Compare two files and find a matching recordsSpring Batch比较两个文件并找到匹配的记录
【发布时间】:2015-02-13 15:16:36
【问题描述】:

我们是否可以根据任何特定列比较两个不同的文件,并使用 Spring Batch 从一个文件中获取值。

例如:

文件 1 内容:

FirstName, LastName, Age

文件 2 内容:

FirstName, LastName, Business

我的要求是基于 FirstName 和 LastName,我需要获取 Business 字段。基本上我会迭代文件 1 并在文件 2 中搜索以检查匹配记录。

目前我正在做的是使用 Apache Lucene 索引文件 2 并使用 Spring Batch 迭代文件 1 并在 Lucene 索引中搜索以获取匹配的文档。

我正在寻找使用 Spring Batch 或任何其他框架的类似功能?

问候, 尚卡尔

【问题讨论】:

  • 您的输入文件是否按名字、姓氏排序?在这种情况下,您可以创建一个同时遍历两个文件的自定义 ItemReader。
  • 其实这不是我的文件内容..我只是举个例子..我们的文件内容更多的是字母数字组合,并且没有排序...感谢您的信息。我们可以用 Composite Item Reader 做些什么吗?
  • 否,如果输入文件未排序,那么我相信您当前将其中一个文件写入数据存储的方法是一个完全可行的选择。
  • @Jimmy Praet.. 只是想如何编写一个自定义阅读器来同时读取文件.. 我阅读 Composite ItemReader 将同时读取多个文件
  • @Jimmy Praet...如果我按排序顺序获取文件会比我目前的方法更快吗?如果是,我如何在 process 方法中同时获取两条记录?

标签: java spring-batch


【解决方案1】:

如果你想走排序输入文件的路线,这样的事情应该可以工作:

public class MergingItemReader implements ItemStreamReader<MergedRecord> {

    private ItemStreamReader<RecordTypeA> readerA;
    private ItemStreamReader<RecordTypeB> readerB;

    @Override
    public MergedRecord read() throws Exception {
        RecordTypeA itemA = readerA.read();
        RecordTypeB itemB = readerB.read();
        Assert.isTrue(itemA.getKey().equals(itemB.getKey()), "Inconsistent data");
        return new MergedRecord(itemA, itemB);
    }

    @Override
    public void open(ExecutionContext executionContext) throws ItemStreamException {
        readerA.open(executionContext);
        readerB.open(executionContext);
    }

    @Override
    public void update(ExecutionContext executionContext) throws ItemStreamException {
        readerA.update(executionContext);
        readerB.update(executionContext);
    }

    @Override
    public void close() throws ItemStreamException {
        readerA.close();
        readerB.close();
    }

    public void setReaderA(ItemStreamReader<RecordTypeA> readerA) {
        this.readerA = readerA;
    }

    public void setReaderB(ItemStreamReader<RecordTypeB> readerB) {
        this.readerB = readerB;
    }

}

关于您关于CompositeItemReader 的其他问题:没有这样的事情。也许你把它和CompositeItemWriter 混淆了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 2013-04-27
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多