gpt4 book ai didi

java - 将多个 CSV 映射到单个 POJO

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:34 25 4
gpt4 key购买 nike

我有许多具有不同列标题的 CSV 文件。目前,我正在读取这些 csv 文件,并根据列标题将它们映射到不同的 POJO 类。因此,某些 CSV 文件具有大约 100 个列标题,这使得创建 POJO 类变得困难。

那么有没有什么技术可以让我使用单个pojo,这样在读取这些csv文件时可以映射到单个POJO类,或者我应该逐行读取CSV文件并进行相应的解析,或者我应该在运行时创建POJO(javaassist)?

最佳答案

如果我正确理解您的问题,您可以使用uniVocity-parsers处理这个并获取 map 中的数据:

//First create a configuration object - there are many options 
//available and the tutorial has a lot of examples
CsvParserSettings settings = new CsvParserSettings();
settings.setHeaderExtractionEnabled(true);

CsvParser parser = new CsvParser(settings);
parser.beginParsing(new File("/path/to/your.csv"));

// you can also apply some transformations:
// NULL year should become 0000
parser.getRecordMetadata().setDefaultValueOfColumns("0000", "Year");

// decimal separator in prices will be replaced by comma
parser.getRecordMetadata().convertFields(Conversions.replace("\\.00", ",00")).set("Price");

Record record;
while ((record = parser.parseNextRecord()) != null) {
Map<String, String> map = record.toFieldMap(/*you can pass a list of column names of interest here*/);
//for performance, you can also reuse the map and call record.fillFieldMap(map);
}

或者您甚至可以解析文件并在一个步骤中获取不同类型的 bean。操作方法如下:

CsvParserSettings settings = new CsvParserSettings();

//Create a row processor to process input rows. In this case we want
//multiple instances of different classes:
MultiBeanListProcessor processor = new MultiBeanListProcessor(TestBean.class, AmountBean.class, QuantityBean.class);

// we also need to grab the headers from our input file
settings.setHeaderExtractionEnabled(true);

// configure the parser to use the MultiBeanProcessor
settings.setRowProcessor(processor);

// create the parser and run
CsvParser parser = new CsvParser(settings);

parser.parse(new File("/path/to/your.csv"));

// get the beans:
List<TestBean> testBeans = processor.getBeans(TestBean.class);
List<AmountBean> amountBeans = processor.getBeans(AmountBean.class);
List<QuantityBean> quantityBeans = processor.getBeans(QuantityBean.class);

查看示例 herehere

如果您的数据太大并且无法将所有内容保存在内存中,您可以使用 MultiBeanRowProcessor 逐行流式传输输入。反而。方法rowProcessed(Map<Class<?>, Object> row, ParsingContext context)将为您提供为当前行中的每个类创建的实例映射。在方法内部,只需调用:

AmountBean amountBean = (AmountBean) row.get(AmountBean.class);
QuantityBean quantityBean = (QuantityBean) row.get(QuantityBean.class);
...

//perform something with the instances parsed in a row.

希望这有帮助。

免责声明:我是这个库的作者。它是开源且免费的(Apache 2.0 许可证)

关于java - 将多个 CSV 映射到单个 POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37462382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com