Univocity - 将每个TSV文件行解析为不同类型的类对象

时间:2016-07-15 05:42:20

标签: univocity

我有一个tsv文件,它有固定的行但每行都映射到不同的Java类。

例如。

recordType  recordValue1
recordType  recordValue1 recordValue2

for first row我有以下课程:

public class FirstRow implements ItsvRecord {

    @Parsed(index = 0)
    private String recordType;

    @Parsed(index = 1)
    private String recordValue1;

    public FirstRow() {
    }
}

对于第二行我有:

public class SecondRow implements ItsvRecord {

    @Parsed(index = 0)
    private String recordType;

    @Parsed(index = 1)
    private String recordValue1;

    public SecondRow() {
    }
}

我想直接将TSV文件解析为相应的对象,但我没有想法。

1 个答案:

答案 0 :(得分:1)

使用InputValueSwitch。这将匹配每行特定列中的值,以确定要使用的RowProcessor。例如:

为您需要处理的每种类型的记录创建两个(或更多)处理器:

final BeanListProcessor<FirstRow> firstProcessor = new BeanListProcessor<FirstRow>(FirstRow.class);
final BeanListProcessor<SecondRow> secondProcessor = new BeanListProcessor<SecondRow>(SecondRow.class);

创建InputValueSwitch

//0 means that the first column of each row has a value that 
//identifies what is the type of record you are dealing with
InputValueSwitch valueSwitch = new InputValueSwitch(0);

//assigns the first processor to rows whose first column contain the 'firstRowType' value
valueSwitch.addSwitchForValue("firstRowType", firstProcessor);

//assigns the second processor to rows whose first column contain the 'secondRowType' value
valueSwitch.addSwitchForValue("secondRowType", secondProcessor);

像往常一样解析:

TsvParserSettings settings = new TsvParserSettings(); //configure...
// your row processor is the switch
settings.setProcessor(valueSwitch);

TsvParser parser = new TsvParser(settings);

Reader input = new StringReader(""+
        "firstRowType\trecordValue1\n" +
        "secondRowType\trecordValue1\trecordValue2");

parser.parse(input);

从处理器中获取已解析的对象:

List<FirstRow> firstTypeObjects = firstProcessor.getBeans();
List<SecondRow> secondTypeObjects = secondProcessor.getBeans();

输出为*:

[FirstRow{recordType='firstRowType', recordValue1='recordValue1'}]

[SecondRow{recordType='secondRowType', recordValue1='recordValue1', recordValue2='recordValue2'}]
  • 假设您在课程中实现了理智的toString()

如果要管理已解析的对象之间的关联:

如果您的FirstRow应包含为SecondRow类型的记录解析的元素,只需覆盖rowProcessorSwitched方法:

    InputValueSwitch valueSwitch = new InputValueSwitch(0) {
    @Override
    public void rowProcessorSwitched(RowProcessor from, RowProcessor to) {
        if (from == secondProcessor) {
            List<FirstRow> firstRows = firstProcessor.getBeans();
            FirstRow mostRecentRow = firstRows.get(firstRows.size() - 1);

            mostRecentRow.addRowsOfOtherType(secondProcessor.getBeans());
            secondProcessor.getBeans().clear();
        }
    }
};
  • 以上假设您的FirstRow班级有一个addRowsOfOtherType方法,该方法会将SecondRow列为参数。

那就是它!

您甚至可以混合搭配其他类型的RowProcessor。还有另一个例子here来证明这一点。

希望这有帮助。

相关问题