简体   繁体   中英

Java ETL - Mapping of CSV Files to POJO for Hibernate Loading via Mapping

I have a couple of different csv files that contain data that I need to import into a table.

Both csv files have fields that map to same object, but have different column names and ordering of the columns. I'm hoping to find an easy way to do this mapping, then use hibernate to handle the writes to the db.

Is there a standard/easy way to do these mappings?

I figure I could create some hashmaps to track the columns and values, that way the columns could come in any order, but I'm curious if there is a simpler (or more standard) way. I didn't find anything in my googles.

Get univocty-parsers and annotate your pojo with the @Parsed annotation, specifying the column name.

public class MyPojo {

    @Trim
    @LowerCase
    @Parsed // column name will be derived from input CSV
    private String text;


    @Parsed(field = "purchase amount") //here you define the column name explictly
    private BigDecimal amount;

    public BigDecimal getAmount(){
        return amount;    
    }
}

Then, extend this class and provide another field names using the annotations:

public class AnotherPojo extends MyPojo {

    @Parsed(field = "some amount")
    private BigDecimal amount;

    @Override
    public BigDecimal getAmount(){
        return amount;    
    }
}

Parse your file with using a row processor of type BeanListProcessor<MyPojo> and then BeanListProcessor<AnotherPojo> (check the tutorial). The code should look like this (I didn't test this but you should get the idea):

public <T> List<T> parseBeans(Class<T> beanType, File inputFile){
    BeanListProcessor<T> rowProcessor = new BeanListProcessor<T>(beanType);
    CsvParserSettings parserSettings = new CsvParserSettings();
    parserSettings.setRowProcessor(rowProcessor);

    parserSettings.setHeaderExtractionEnabled(true); //will get headers from the input file
    CsvParser parser = new CsvParser(parserSettings);

    //this will read your file and submit all rows to the row processor defined above
    parser.parse(new FileReader(inputFile));
    List<T> beans = rowProcessor.getBeans();
    return beans;
}

As all objects will be in the hierarchy of MyPojo , you can easily persist the data extracted from each file.

Disclosure: I am the author of this library. It's open-source and free (Apache V2.0 license).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM