Spring Batch to Compare two files and find a matching records

跟風遠走 提交于 2021-02-10 23:45:58

问题


Is there anyway we can compare two different files based on any specific column and get the values from one file using Spring Batch.

For example:

File 1 Content:

FirstName, LastName, Age

File 2 Content:

FirstName, LastName, Business

My requirement is something like based on FirstName and LastName i need to get the Business field. Basically i will iterate File 1 and search in File 2 to check for matching record.

Currently what i am doing is Index the File 2 using Apache Lucene and iterate the file 1 using Spring Batch and search in Lucene Index to get the matched documents.

I am looking something similar functionality using Spring Batch or any other frameworks?

Regards, Shankar


回答1:


Something like this should work if you want to go the route of sorted input files:

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;
    }

}

Regarding your other question about CompositeItemReader: there's no such thing. Maybe you are confusing it with CompositeItemWriter.



来源:https://stackoverflow.com/questions/28502712/spring-batch-to-compare-two-files-and-find-a-matching-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!