用特定的POJO字段映射csv文件的特定列

时间:2019-05-09 12:57:23

标签: java opencsv univocity

我必须根据具有特定POJO属性的索引来映射特定CSV列。映射将基于json文件,该文件将包含columnIndex和属性名称,这意味着对于csv文件中的特定columnIndex,您必须映射Pojo类中的特定属性。 下面是一个json文件示例,其中显示了具有Pojo属性的列映射策略。

  

[{“ index”:0,“ columnname”:“ date”},{“ index”:1,“ columnname”:“ deviceAddress”},{“ index”:7,“ columnname”:“ iPAddress” },{“ index”:3,“ columnname”:“ userName”}},{“ index”:10,“ columnname”:“ group”},{“ index”:5,“ columnname”:“ eventCategoryName”}, {“ index”:6,“列名”:“消息”}]

我尝试过使用OpenCSV库,但是我无法使用它读取部分列,这是我面临的挑战。如上面的json所示,您可以看到我们正在跳过索引2和4来从CSV文件读取。下面是带有openCSV文件的代码。

public static List<BaseDataModel> readCSVFile(String filePath,List<String> columnListBasedOnIndex) {
        List<BaseDataModel> csvDataModels = null;
        File myFile = new File(filePath);
        try (FileInputStream fis = new FileInputStream(myFile)) {
            final ColumnPositionMappingStrategy<BaseDataModel> strategy = new ColumnPositionMappingStrategy<BaseDataModel>();
            strategy.setType(BaseDataModel.class);


            strategy.setColumnMapping(columnListBasedOnIndex.toArray(new String[0]));

            final CsvToBeanBuilder<BaseDataModel> beanBuilder = new CsvToBeanBuilder<>(new InputStreamReader(fis));
            beanBuilder.withMappingStrategy(strategy);

            csvDataModels = beanBuilder.build().parse();

        } catch (Exception e) {
            e.printStackTrace();
        }
}


List<ColumnIndexMapping> columnIndexMappingList = dataSourceModel.getColumnMappingStrategy();
                    List<String> columnNameList = columnIndexMappingList.stream().map(ColumnIndexMapping::getColumnname)
                            .collect(Collectors.toList());

List<BaseDataModel> DataModels = Utility
                                    .readCSVFile(file.getAbsolutePath() + File.separator + fileName, columnNameList);

我也曾尝试用univocity,但是有了这个库,我如何才能将具有特定属性的csv映射。下面是代码-

CsvParserSettings settings = new CsvParserSettings();
        settings.detectFormatAutomatically(); //detects the format 
        settings.getFormat().setLineSeparator("\n");
        //extracts the headers from the input
        settings.setHeaderExtractionEnabled(true);
        settings.selectIndexes(0, 2); //rows will contain only values of columns at position 0 and 2
        CsvRoutines routines = new CsvRoutines(settings); // Can also use TSV and Fixed-width routines
        routines.parseAll(BaseDataModel.class, new File("/path/to/your.csv"));


        List<String[]> rows = new CsvParser(settings).parseAll(new File("/path/to/your.csv"), "UTF-8");

请看看在这种情况下是否有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

此处是univocity解析器的作者。您可以在代码中而不是注释中定义到类属性的映射。像这样:

public class BaseDataModel {
    private String a;
    private int b;
    private String c;
    private Date d;
}

然后在代码上,将属性映射到所需的任何列名称:

ColumnMapper mapper = routines.getColumnMapper();
mapper.attributeToColumnName("a", "col1");
mapper.attributeToColumnName("b", "col2");
mapper.attributeToColumnName("c", "col3");
mapper.attributeToColumnName("d", "col4");

您还可以使用mapper.attributeToIndex("d", 3);将属性映射到给定的列索引。

希望这会有所帮助。