apache-camelbindy

Camel - Enrich CSV from FTP with CSV from local disk using Camel Bindy


The goal is to produce a report every hour by comparing two CSV files with use of Camel 3.0.0. One is located on a FTP server, the other on disk. How to use poll enrich pattern in combination with unmarshalling the CSV on disk with Bindy Dataformat?

Example code (for simplicity the FTP endpoint is replaced by a file endpoint):

@Component
public class EnricherRoute extends RouteBuilder {
    @Override
    public void configure() {
        from("file://data?fileName=part_1.csv&scheduler=quartz2&scheduler.cron=0+0+0/1+*+*+?")
            .unmarshal().bindy(BindyType.Csv, Record.class)
            .pollEnrich("file://data?fileName=part_2.csv", new ReportAggregationStrategy())
            .marshal().bindy(BindyType.Csv, Record.class)
            .to("file://reports?fileName=report_${date:now:yyyyMMdd}.csv");

    }
}

The problem in this example is that in the ReportAggregationStrategy the resource (coming from data/part_2.csv, see below) is not unmarshalled. How to unmarshal data/part_2.csv as well?

public class ReportAggregationStrategy implements AggregationStrategy {
    @Override
    public Exchange aggregate(Exchange original, Exchange resource) {
        final List<Record> originalRecords = original.getIn().getBody(List.class);
        final List<Record> resourceRecords = resource.getIn().getBody(List.class); // Results in errors!

        ...
    }
}

Solution

  • You can wrap enrichment with direct endpoint and do unmarshaling there.

    from("file://data?fileName=part_1.csv&scheduler=quartz2&scheduler.cron=0+0+0/1+*+*+?")
        .unmarshal().bindy(BindyType.Csv, Record.class)
        .enrich("direct:enrich_record", new ReportAggregationStrategy())
        .marshal().bindy(BindyType.Csv, Record.class)
        .to("file://reports?fileName=report_${date:now:yyyyMMdd}.csv");
    
    from("direct:enrich_record")
        .pollEnrich("file://data?fileName=part_2.csv")
        .unmarshal().bindy(BindyType.Csv, Record.class);