gpt4 book ai didi

java - 如何在 Spring 批处理中使用 FlatFileItemReader 忽略 CSV 中不需要的列

转载 作者:搜寻专家 更新时间:2023-10-31 19:53:42 24 4
gpt4 key购买 nike

我遇到了一个问题,我有一个包含 10 列的 CSV 文件,只需要将选定的列映射到我的 Java 对象。但是,CSV 是标题栏,数据位置是固定的。所以我知道只有第 1 到 3 列对我有用,其余的必须忽略。例如:CSV 是:A1,A2,A3,A4,A5,A6,A7,A8,A9,A10

我只需要将 A1 到 A3 列映射到我的 pojo。我确定这不是正确的方法,但我尝试过这样做,但没有用。 Spring Batch 正在尝试将所有列值映射到我的 pojo。

<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names" value="Name,Department,Age,,,,,,," />
</bean>
</property>

<property name="fieldSetMapper">
<bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="prototypeBeanName" value="EmployeeDO" />
</bean>

我还没有探索过 spring batch 的所有功能,但是有什么可以轻松实现的吗?任何帮助将不胜感激

最佳答案

DelimitedLineTokenizer.setIncludedFields()应该是解决您的问题的正确选择。

The fields to include in the output by position (starting at 0). By default all fields are included, but this property can be set to pick out only a few fields from a larger set. Note that if field names are provided, their number must match the number of included fields.

关于java - 如何在 Spring 批处理中使用 FlatFileItemReader 忽略 CSV 中不需要的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33972200/

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