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

u0njafvf  于 2023-11-14  发布在  Java
关注(0)|答案(2)|浏览(149)

我试图在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)


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

xqnpmsa8

xqnpmsa81#

我刚刚尝试了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许可证)。

ht4b089n

ht4b089n2#

如果你可以自己构建它,那么就获取opencsv的 Backbone.js 构建。我修改了CsvToBean,打印出错误发生时它所在的行号。

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

字符串
这将在3.6版本中发布,除非有任何问题,应该会在感恩节之前发布。
我很好奇是什么错误导致了这个错误。如果你再往下看一下你的异常堆栈跟踪,你应该会看到一个“Caused by:“,这是由CsvToBean捕获的异常。考虑到你有超过20万行,我怀疑你得到了一个内存不足的异常-这意味着你要么需要将文件拆分成更小的文件,增加JVM的内存,要么使用3.5版本中引入的IterableCsvToBean,它允许逐行解析。
希望能帮上忙。
:)

相关问题