我想使用camel-csv
下载并解析一个大型CSV,但我无法找到一个我满意的解决方案。 camel-csv
似乎旨在读取和处理放置在磁盘上的文件。
我想通过HTTP下载URL列表,并在下载时解析流。我可以绕过camel-csv
这样做:
from("mock:in").process(new TaxiDataProcessor(new DataCSVParserFactory())).to("mock:out");
public class DataProcessor implements Processor {
private final DataCSVParserFactory csvParserFactory;
@Inject
public DataProcessor(DataCSVParserFactory csvParserFactory) {
this.csvParserFactory = csvParserFactory;
}
@Override
public void process(Exchange exchange) throws Exception {
String file = (String) exchange.getIn().getBody();
URL url = new URL(file);
CSVParser parser = csvParserFactory.build(url);
for (CSVRecord csvRecord : parser) {
exchange.getIn().setBody(csvRecord);
}
}
}
但是可以使用像camel-ahc
这样的东西下载文件并将其输入csv unmarshalling吗?类似的东西:
from("direct:input").unmarshall().csv().to("direct:out");
template.send("ahc:uri");
答案 0 :(得分:0)
Camel-csv用于编组和解组csv。要从某个网址下载文件,您需要其他组件,例如camel-netty4-http
。
一个简单的例子:
from("netty4-http:http://localhost:8080/foo")
.marshal().csv()
.log("${body}");
您可能需要在编组之前将其转换为String。
编辑:
好的下载多个文件,你需要一些方法来触发你的路线。最简单的是计时器,但使用你喜欢的任何东西。然后你可以使用toD()这是一个动态路由器并在那里注入你的网址。如果你想重复这个过程,你需要拆分然后注入。以下示例(未经测试)以帮助您入门:
//Create the list of urls any way you like. This is just to show the principle. You can create them in a bean and inject them in a Camel header if you like.
String listOfUrls = "url1, url2, url3";
from("timer:foo?period=5000")
.setHeader("urls", constant(listOfUrls))
.split(header("urls")) //split url is part of body now
.toD("${{body}") //take the url from the body and use that as a uri
.log("${body}");
注意,如果您打算使用camel-http4组件发送请求,则仍需要它。 http://camel.apache.org/splitter.html 请参见dynamicTo: http://camel.apache.org/message-endpoint.html