我正在尝试在 Eclipse 中使用 OpenCSV 解析大型 CSV 文件。 这是 CSV 文件的前 4 条记录。完整文件有 219,590 条记录:-
0,23,1,0,[email protected],"Construction/Contractors/Contractors"
0,43,1,0,[email protected],"Engineering/Electrical Engineering/Electrical Engineering"
0,395,1,0,[email protected],"Sales/Sales Force Management/Sales Management"
0,398,1,0,[email protected],"Sales/Sales Strategy/Sales"
这是 Java 代码:-
File csvFile = new File("data/userattrib2_30day.csv");
ColumnPositionMappingStrategy strategy = new ColumnPositionMappingStrategy();
strategy.setType(UserAttribRecord.class);
String[] columns = new String[] {"userId", "attributeId", "rating", "timestamp", "email", "attributeDesc"};
strategy.setColumnMapping(columns);
CSVReader reader = new CSVReader(new FileReader(csvFile));
CsvToBean<UserAttribRecord> csv = new CsvToBean<UserAttribRecord>();
List<UserAttribRecord> userAttribList = csv.parse(strategy,reader);
它失败了:-
Exception in thread "main" java.lang.RuntimeException: Error parsing CSV!
at com.opencsv.bean.CsvToBean.parse(CsvToBean.java:95)
at com.opencsv.bean.CsvToBean.parse(CsvToBean.java:75)
如何找出错误所在?似乎没有任何调试信息,所以我不知道哪条记录的哪个字段有错误。我该如何调试这个? 谢谢
如果您可以自己构建它,请获取 opencsv 的主干构建。我已经修改了 CsvToBean 以打印出错误发生时它所在的行号。
throw new RuntimeException("Error parsing CSV line: " + lineProcessed + " values: " + Arrays.toString(line), e);
这将在 3.6 版本中发布,如果没有任何问题,应该会在感恩节之前发布。
我很好奇导致此错误的原因是什么。如果您进一步查看异常堆栈跟踪,您应该会看到“Caused by:”,这就是 CsvToBean 捕获的异常。鉴于您有超过 200k 行,我怀疑您遇到了内存不足异常 - 这意味着您需要将文件拆分为更小的文件,增加 JVM 的内存,或者使用 3.5 版本中引入的 IterableCsvToBean允许逐行解析。
希望有帮助。
:)
我刚刚尝试使用 univocity-parsers,我可以毫无问题地解析您的示例输入。尝试一下,因为它比 OpenCSV 快两倍(平均快 2 倍)。这是我的代码:
首先将
@Parsed
注释添加到要从 CSV 加载的字段(有很多可用选项,请查看文档)。
public static class UserAttributeRecord{
@Parsed
int userId;
@Parsed
int attributeId;
@Parsed
int rating;
@Parsed
long timestamp;
@Parsed
String email;
@Parsed
String attributeDesc;
}
这是解析文件所需的代码:
public static void main(String[] args) throws IOException{
//creates a processor of java beans.
BeanListProcessor<UserAttributeRecord> beanProcessor = new BeanListProcessor<UserAttributeRecord>(UserAttributeRecord.class);
//then a settings object to configure the parser
CsvParserSettings settings = new CsvParserSettings(); //many options here, check the tutorial
//configures the parser to use the bean processor.
settings.setRowProcessor(beanProcessor);
//configures the input format.
settings.setHeaders("userId", "attributeId", "rating", "timestamp", "email", "attributeDesc");
settings.getFormat().setLineSeparator("\n");
//creates a parser with your settings
CsvParser parser = new CsvParser(settings);
//parses everything. All rows are submitted to the row processor defined above
parser.parseAll(new FileReader(new File("/path/to/file.csv")));
//here's your list of beans
List<UserAttributeRecord> beans = beanProcessor.getBeans();
}
披露:我是这个库的作者。它是开源且免费的(Apache V2.0 许可证)。