我有一个java服务器应用程序下载CSV文件并解析它。解析可能需要5到45分钟,并且每小时发生一次。此方法是应用程序的瓶颈,因此它不是过早的优化。到目前为止的代码:快速CSV解析
client.executeMethod(method);
InputStream in = method.getResponseBodyAsStream(); // this is http stream
String line;
String[] record;
reader = new BufferedReader(new InputStreamReader(in), 65536);
try {
// read the header line
line = reader.readLine();
// some code
while ((line = reader.readLine()) != null) {
// more code
line = line.replaceAll("\"\"", "\"NULL\"");
// Now remove all of the quotes
line = line.replaceAll("\"", "");
if (!line.startsWith("ERROR"){
//bla bla
continue;
}
record = line.split(",");
//more error handling
// build the object and put it in HashMap
}
//exceptions handling, closing connection and reader
是否有任何现有的库,可以帮助我加快速度?我可以改进现有的代码吗?
文件有多大?你有没有尝试分析你的代码?这会给你提供瓶颈和清晰的想法。如果您的网络成为主要问题,我不会感到惊讶。也可以查看http://commons.apache.org/sandbox/csv/而不是自己构建解析器。 – joostschouten
我现在正在分析,我知道大部分时间都是由网络连接引起的。我想首先改善解析,因为在网络中我需要改变架构。 (我的估计是,更快的解析可以提高10-15%的加载时间)。 –
听起来公平。请使用csv解析器,因为这些解析器已经过优化,您肯定会遇到逃跑和国际化的问题,您不必担心。祝你好运。 – joostschouten