csv 如何在Univocity中使用BatchedColumnProcessor的列Map

avkwfej4  于 2023-03-10  发布在  其他
关注(0)|答案(1)|浏览(125)

bounty将在5天后过期。回答此问题可获得+50声望奖励。Salman S希望引起更多人关注此问题。

如何通过在univocity中使用BatchedColumnProcessor来使用列Map?

CsvParserSettings settings = new CsvParserSettings();
    settings.setProcessor(new BatchedColumnProcessor(5) {
        @Override
        public void batchProcessed(int rowsInThisBatch) {}
    });
    CsvParser parser = new CsvParser(settings);

就像我们在BeanListProcessor中使用的那样,并添加自定义列Map

BeanListProcessor<Product> rowProcessor = new BeanListProcessor<Product>(Product.class);
rowProcessor.setColumnMapping();
ht4b089n

ht4b089n1#

X1 M0 N1 X仅在X1 M1 N1 X第287期中提及一次,并在X1 E0 F1 X中说明,其应用于具有OpenCSV的X1 M2 N1 X。
该问题提到了“initial code to support github issue #287 - map column name to attribute“,并以Github_287.java为例
因此,从2.8.0开始:
我发布了一个2.8.0-SNAPSHOT版本,它允许您从以下位置调用getColumnMapper()

any *Routines class
any *Bean*Processor class (including BeanWriterProcessor) for writing

mapper.attributeToIndex("name", 0); //name goes to the first column
mapper.attributeToColumnName("name", "client_name"); .// same thing, but the first column has header "client_name"

嵌套类也是受支持的,你可以做如下的事情,其中name被隐藏在一个对象结构中(假设一个类Purchase有一个Buyer属性,而这个属性又有一个Contact属性,name位于这个属性中):

mapper.methodToIndex("buyer.contact.name", 0); 

// use methods too. This assumes Contact has a `fullName(java.lang.String)` setter method:
mapper.methodToColumnName("buyer.contact.fullName", String.class, "client_name");

// this maps getter and setter methods named `fullName` to column `client_name`. The getters or setters are used depending if you are

向文件写入或从文件阅读。Map器。方法到列名(“买方。联系人。全名”,“客户_名称”);
你也可以给予它一张Map:

Map<String, Integer> mappings = new HashMap<String, Integer>();
... fill your map
mapper.attributesToIndexes(mappings);

相关问题