如何在 apache camel DSL 中使用基于包含的路由解析 CSV 数据?

How to parse CSV data using contain based routing in apache camel DSL?

我正在编写一个 DSL 路由读取不同 CSV 文件的路由。 我正在根据 header 过滤 CSV 并对其执行一些操作,如解组。 我的路线:

CsvDataFormat csv = new CsvDataFormat();    

            //Route 1 for filter CSV based on header
            from("file:/home/r2/Desktop/csvFile?noop=true")
                .choice().when(body().contains("partyName"))
                    .to("direct:partyNameCSV")
                .when(body().contains("\"stuffName\""))
                    .to("direct:stuffNameCSV")
                .otherwise().endChoice();   

            //Route 2 partyNameCSV
            from("direct:partyNameCSV")
                .unmarshal(csv)
                .process(new PartyNameCSVProcessor())
                .end();

            //Route 3 stuffNameCSV
            from("direct:stuffNameCSV")
                .unmarshal(csv)
                .process(new StuffCSVProcessor())
                .end();

输出:构建成功但未调用任何处理器

[CSV.map.AutoCSV.main()] INFO org.apache.camel.impl.DefaultCamelContext - Route: route1 started and consuming from: file:///home/r2/Desktop/csvFile?noop=true [CSV.map.AutoCSV.main()] INFO org.apache.camel.impl.DefaultCamelContext - Route: route2 started and consuming from: direct://partyNameCSV [CSV.map.AutoCSV.main()] INFO org.apache.camel.impl.DefaultCamelContext - Route: route3 started and consuming from: direct://stuffNameCSV [CSV.map.AutoCSV.main()] INFO org.apache.camel.impl.DefaultCamelContext - Total 3 routes, of which 3 are started [CSV.map.AutoCSV.main()] INFO org.apache.camel.impl.DefaultCamelContext - Apache Camel 2.20.1 (CamelContext: camel-1) started in 0.638 seconds [CSV.map.AutoCSV.main()] INFO org.apache.camel.impl.DefaultCamelContext - Apache Camel 2.20.1 (CamelContext: camel-1) is shutting down [CSV.map.AutoCSV.main()] INFO org.apache.camel.impl.DefaultShutdownStrategy - Starting to graceful shutdown 3 routes (timeout 300 seconds) [Camel (camel-1) thread #2 - ShutdownTask] INFO org.apache.camel.impl.DefaultShutdownStrategy - Route: route3 shutdown complete, was consuming from: direct://stuffNameCSV [Camel (camel-1) thread #2 - ShutdownTask] INFO org.apache.camel.impl.DefaultShutdownStrategy - Route: route2 shutdown complete, was consuming from: direct://partyNameCSV [Camel (camel-1) thread #2 - ShutdownTask] INFO org.apache.camel.impl.DefaultShutdownStrategy - Route: route1 shutdown complete, was consuming from: file:///home/r2/Desktop/csvFile?noop=true

我的问题是我哪里做错了或者我遗漏了什么?

更新

file1.csv

"partyName","partNumber" "rajat temaniya","1" "lalit kumar","2"

file2.csv

"stuffName","price" "laptop","88000" "mobile","50000"

在进行任何基于内容的路由之前使用 convertBodyTo(String.class) 转换您的正文。

您可以启用流缓存以便保留正文供以后使用,并建议将消息​​转换为字符串。

from({{consumer}})
    .streamCaching()
    .to({{producer}});