如何在 spring 批次中使用 FlatFileItemReader 忽略 CSV 中不需要的列
How to ignore unwanted columns in CSV using FlatFileItemReader in spring batch
我 运行 遇到一个问题,我有一个包含 10 列的 CSV 文件,只需要将选定的列映射到我的 Java objects。但是 CSV 是 header 列并且数据位置是固定的。所以我知道只有第 1 到 3 列对我有用,其余的必须忽略。
例如:
CSV 是:
A1,A2,A3,A4,A5,A6,A7,A8,A9,A10
我只需要将 A1 到 A3 列映射到我的 pojo。
我确定这不是正确的方法,但我尝试过这样做,但没有用。 Spring 批处理正在尝试将所有列值映射到我的 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 批处理的所有功能,但是有什么可以轻松实现的吗?任何帮助将不胜感激
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.
我 运行 遇到一个问题,我有一个包含 10 列的 CSV 文件,只需要将选定的列映射到我的 Java objects。但是 CSV 是 header 列并且数据位置是固定的。所以我知道只有第 1 到 3 列对我有用,其余的必须忽略。 例如: CSV 是: A1,A2,A3,A4,A5,A6,A7,A8,A9,A10
我只需要将 A1 到 A3 列映射到我的 pojo。 我确定这不是正确的方法,但我尝试过这样做,但没有用。 Spring 批处理正在尝试将所有列值映射到我的 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 批处理的所有功能,但是有什么可以轻松实现的吗?任何帮助将不胜感激
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.