Java CsvToBean.解析CSV失败,出现解析CSV错误



我试图在Eclipse中使用OpenCSV解析一个大型CSV文件。下面是CSV文件的前4条记录。完整的文件有219,590条记录:-

0,23,1,0,someone@email.com,"Construction/Contractors/Contractors"
0,43,1,0,someone@email.com,"Engineering/Electrical Engineering/Electrical Engineering"
0,395,1,0,someone@email.com,"Sales/Sales Force Management/Sales Management"
0,398,1,0,someone@email.com,"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)

我怎么知道错误是什么?似乎没有任何调试信息,所以我不知道哪个字段的记录有一个错误。我如何调试这个?由于

如果您可以自己构建它,请获取opensv的主干构建。我已经修改了CsvToBean,以打印出错误发生时所处的行号。

throw new RuntimeException("Error parsing CSV line: " + lineProcessed + " values: " + Arrays.toString(line), e);

这将在3.6版本中发布,排除任何问题,应该在感恩节之前发布。

我很好奇是什么错误导致了这一点。如果您再往下看一下异常堆栈跟踪,您应该看到一个"由:引起的",这就是CsvToBean捕获的异常。假设您有超过200k行的代码,我怀疑您正在遇到内存不足异常——这意味着您要么需要将文件分割成更小的文件,增加JVM的内存,要么使用3.5版本中引入的IterableCsvToBean,它允许逐行解析。

希望有帮助。

:)

我刚刚尝试了单一性解析器,我可以解析您的示例输入而没有任何问题。试试吧,因为它比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许可)。

相关内容

  • 没有找到相关文章

最新更新