1

How can i use column mapping by using BatchedColumnProcessor in univocity?

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

like we use in BeanListProcessor and add custom column mapping

BeanListProcessor<Product> rowProcessor = new BeanListProcessor<Product>(Product.class);
rowProcessor.setColumnMapping();
Salman S
  • 47
  • 1
  • 15

1 Answers1

0

setColumnMapping is only mentioned once in univocity/univocity-parsers issue 287, and illustrated in this answer, applied to a ColumnPositionMappingStrategy with opencsv.

That issue mentions "initial code to support github issue #287 - map column name to attribute", with an example at Github_287.java

So from 2.8.0:

I've released a 2.8.0-SNAPSHOT build which allows you to call getColumnMapper() from:

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"

Nested classes are also supported, you can do stuff such as the following, where name is buried in an object structure (assume a class Purchase with a Buyer attribute, which in turn has a Contact attribute, where the name is located):

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

writing to a file or reading from it. mapper.methodToColumnName("buyer.contact.fullName", "client_name");

You can also just give it a map:

Map<String, Integer> mappings = new HashMap<String, Integer>();
... fill your map
mapper.attributesToIndexes(mappings);
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250