【发布时间】:2020-01-03 03:33:44
【问题描述】:
我正在尝试在 Spring Batch 中使用 Univocity 解析器。我面临的问题是如何整合它们。
Spring Batch Chunk Steps 遵循给定文件每一行的流程:
我需要在ItemReader 中使用 Univocity。它对输入文件(即 CSV 文件)的每一行执行read() 方法。我唯一做的就是使用BeanListProcessor 来读取项目并将其直接转换为我的Java 对象,返回已解析Bean 的List,但我不想一次加载所有记录,以避免OutOfMemory 异常.我没有找到其他可以帮助我的东西。
我曾尝试使用this 答案作为示例,但无法确定一次返回一项。
@Override
public Address read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
CsvParserSettings parserSettings = new CsvParserSettings();
//settings
CsvRoutines routines = new CsvRoutines(parserSettings);
for (Address address : routines.iterate(Address.class, input, "UTF-8")) {
/*
*here I need to return only the current object,
*the return of this method will be passed as an argument to a processor
*the next time this method is called it has to return the next one
*Could not figure out how to control what is the current.
*/
return ???:
}
return ???;
}
如何在我的 ItemReader 中使用 Univocity 一次读取一行,仍然使用 BeanProcessor 将我的行自动解析为我的 Java 对象?
【问题讨论】:
标签: java csv parsing spring-batch univocity