我有一个 csv 文件 有 2000 行。我希望一次将它拆分为 20 行,并将每行发送到 apache camel 中的处理器。我如何使用 Splitter EIP 做到这一点?请帮助..
我建议花一些时间研究 Apache Camel 文档。例如,在 Splitter EIP 页面上,它全部记录了如何执行此操作。
参见https://camel.apache.org/components/3.20.x/eips/split-eip.html#_splitting_files_by_grouping_n_lines_together部分通过将N行分组在一起分割文件
package com.camel;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dataformat.csv.CsvDataFormat;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.QuoteMode;
public class FileSplitter {
public static void main(String args[]) throws Exception {
CamelContext context = new DefaultCamelContext();
CsvDataFormat csvParser = new CsvDataFormat(CSVFormat.DEFAULT);
csvParser.setSkipHeaderRecord(true);
csvParser.setQuoteMode(QuoteMode.ALL);
context.addRoutes(new RouteBuilder() {
public void configure() {
String fileName = "Hello.csv";
int lineCount = 20;
System.out.println("fileName = " + fileName);
System.out.println("lineCount = " + lineCount);
from("file:data/inbox?noop=true&fileName=" + fileName).unmarshal(csvParser).split(body()).streaming()
.aggregate(constant(true), new ArrayListAggregationStrategy()).completionSize(lineCount)
.completionTimeout(1500).marshal(csvParser)
.to("file:data/outbox?fileName=${file:name.noext}_${header.CamelSplitIndex}.csv");
}
});
context.start();
Thread.sleep(10000);
context.stop();
System.out.println("End");
}
}