Using apache camel csv processor with pollEnrich pattern?

試著忘記壹切 提交于 2020-01-23 00:58:06

问题


Apache Camel 2.12.1

Is it possible to use the Camel CSV component with a pollEnrich? Every example I see is like:

from("file:somefile.csv").marshal...

Whereas I'm using the pollEnrich, like:

pollEnrich("file:somefile.csv", new CSVAggregator())

So within CSVAggregator I have no csv...I just have a file, which I have to do csv processing myself. So is there a way of hooking up the marshalling to the enrich bit somehow...?

EDIT

To make this more general... eg:

from("direct:start")
.to("http:www.blah")
.enrich("file:someFile.csv", new CSVAggregationStrategy) <--how can I call marshal() on this?

...

public class CSVAggregator implements AggregationStrategy {
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) { 
/*  Here I have:
oldExchange = results of http blah endpoint
newExchange = the someFile.csv GenericFile object */

}

Is there any way I can avoid this and use marshal().csv sort of call on the route itself?

Thanks,

Mr Tea


回答1:


You can use any endpoint in enrich. That includes direct endpoints pointing to other routes. Your example...

Replace this:

from("direct:start")
  .to("http:www.blah")
  .enrich("file:someFile.csv", new CSVAggregationStrategy)

With this:

from("direct:start")
  .to("http:www.blah")
  .enrich("direct:readSomeFile", new CSVAggregationStrategy);

from("direct:readSomeFile")
  .to("file:someFile.csv")
  .unmarshal(myDataFormat);



回答2:


I ran into the same issue and managed to solve it with the following code (note, I'm using the scala dsl). My use case was slightly different, I wanted to load a CSV file and enrich it with data from an additional static CSV file.

from("direct:start") pollEnrich("file:c:/data/inbox?fileName=vipleaderboard.inclusions.csv&noop=true") unmarshal(csv)
from("file:c:/data/inbox?fileName=vipleaderboard.${date:now:yyyyMMdd}.csv") unmarshal(csv) enrich("direct:start", (current:Exchange, myStatic:Exchange) => {
    // both exchange in bodies will contain lists instead of the file handles
})

Here the second route is the one which looks for a file in a specific directory. It unmarshals the CSV data from any matching file it finds and enriches it with the direct route defined in the preceding line. That route is pollEnriching with my static file and as I don't define an aggregation strategy it just replaces the contents of the body with the static file data. I can then unmarshal that from CSV and return the data.

The aggregation function in the second route then has access to both files' CSV data as List<List<String>> instead of just a file.



来源:https://stackoverflow.com/questions/20640944/using-apache-camel-csv-processor-with-pollenrich-pattern

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