univocity 解析为 bean 导致空值

univocity parsing into bean results in null value

我正在尝试使用 univocity csv 解析器来解析一个包含超过 300 万行进入 java bean 列表。我像在我的例子中那样设置它,但是当我解析 csv 时,每个 java bean 都有 null 属性值。我尝试了这些设置,但找不到我的错误所在。 这些是我使用的 Maven 依赖项:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.8.1</version>
</dependency>
<dependency>
    <groupId>com.univocity</groupId>
    <artifactId>univocity-parsers</artifactId>
    <version>2.7.6</version>
</dependency>

这是我的测试class:

public class ParserTest {
    public List<OdsTx> start(File file) {
        BeanListProcessor<OdsTx> rowProcessor = new BeanListProcessor<OdsTx>(OdsTx.class);
        CsvParserSettings settings = new CsvParserSettings();
        settings.setDelimiterDetectionEnabled(true, ';');
        settings.setProcessor(rowProcessor);
        CsvParser parser = new CsvParser(settings);
        parser.parse(file);
        return rowProcessor.getBeans();
    }
    public static void main(String[] args) {
        String filename = "input/ods_TX.csv";
        File file = new File(filename);
        int testrow = 3;
        ParserTest test = new ParserTest();
        List<OdsTx> result = test.start(file);
        System.out.println("result size: " + result.size());
        System.out.println(result.get(testrow).toString());
    }

}

这是我的豆子:

public class OdsTx {
    @Parsed(index = 0)
    private String CARDID;
    @Parsed(index = 1)
    private String ACCEPTANCEDATE;
    @Parsed(index = 2)
    private String AMOUNT;
    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }
    public String getCARDID() {
        return CARDID;
    }
    public void setCARDID(String cARDNO) {
        CARDID = cARDNO;
    }
    public String getACCEPTANCEDATE() {
        return ACCEPTANCEDATE;
    }
    public void setACCEPTANCEDATE(String aCCEPTANCEDATE) {
        ACCEPTANCEDATE = aCCEPTANCEDATE;
    }
    public String getAMOUNT() {
        return AMOUNT;
    }
    public void setAMOUNT(String aMOUNT) {
        AMOUNT = aMOUNT;
    }
}

这是我的 csv 文件:

CARDID;ACCEPTANCEDATE;AMOUNT
12345168852;2018-01-01-07.56.29.000000;900
1234100080716;2018-01-01-09.19.26.000000;1000
1234100087256;2018-01-01-09.32.53.000000;1000
1234100087256;2018-01-01-09.33.03.000000;1000
12345199915;2018-01-01-09.41.44.000000;200
12345199915;2018-01-01-09.41.46.000000;200

我使用 toString() 方法得到的结果总是这样:

result size: 6
de.westlotto.connect.mehrfach.model.csv.OdsTx@4b9af9a9[
  CARDNO=<null>
  ACCEPTANCE_DATE=<null>
  AMOUNT=<null>
]

编辑:

我的文件路径有误。现在我收到以下错误:

Exception in thread "main" com.univocity.parsers.common.TextParsingException: java.lang.ArrayIndexOutOfBoundsException - 512
Hint: Number of columns processed may have exceeded limit of 512 columns. Use settings.setMaxColumns(int) to define the maximum number of columns your input can have
Ensure your configuration is correct, with delimiters, quotes and escape sequences that match the input format you are trying to parse
Parser Configuration: CsvParserSettings:
    Auto configuration enabled=true
    Autodetect column delimiter=false
    Autodetect quotes=false
    Column reordering enabled=true
    Delimiters for detection=null
    Empty value=null
    Escape unquoted values=false
    Header extraction enabled=false
    Headers=null
    Ignore leading whitespaces=true
    Ignore leading whitespaces in quotes=false
    Ignore trailing whitespaces=true
    Ignore trailing whitespaces in quotes=false
    Input buffer size=1048576
    Input reading on separate thread=true
    Keep escape sequences=false
    Keep quotes=false
    Length of content displayed on error=-1
    Line separator detection enabled=false
    Maximum number of characters per column=4096
    Maximum number of columns=512
    Normalize escaped line separators=true
    Null value=null
    Number of records to read=all
    Processor=com.univocity.parsers.common.processor.BeanListProcessor
    Restricting data in exceptions=false
    RowProcessor error handler=null
    Selected fields=field selection: [0, 1, 2]
    Skip bits as whitespace=true
    Skip empty lines=true
    Unescaped quote handling=nullFormat configuration:
        CsvFormat:
            Comment character=#
            Field delimiter=;
            Line separator (normalized)=\n
            Line separator sequence=\n
            Quote character="
            Quote escape character="
            Quote escape escape character=null

这里是库的作者。您没有 post 您正在使用什么输入,但我认为您的问题可能来自 settings.detectFormatAutomatically(';');

检查 parser.getDetectedFormat() 检测到的格式。它可能检测到不正确的分隔符。

在一个不相关的注释中,由于您将属性映射到固定位置而不是 header 名称,因此您不需要 @Headers 注释。您也不需要每个属性中的 @Trim 注释,因为默认情况下解析器会为您修剪所有值。