read only selective columns from csv file using spring batch

此生再无相见时 提交于 2019-12-05 13:32:39
arkabhi

Try using a custom fieldSetMapper. You can use it similar to a ResultSet with indexes. You have to list all the column names only if you want automatic mapping. Specify only the delimiter, in your case ","

<bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader"  scope="step">
    <property name="resource" value="YOURFILE" />
     <property name="lineMapper">
         <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
             <property name="fieldSetMapper">
                <bean class="CUSTOMFIELDSETMAPPER" />
            </property>
             <property name="lineTokenizer">
                <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    <property name="delimiter" value="," />
                </bean>
            </property>
         </bean>
     </property>
 </bean>

The Custom Mapper could be something like this, say if you want to read the 1st column and 25th column:

public class CustomMapper implements FieldSetMapper<CustomPOJO>{

    @Override
    public CustomPOJO mapFieldSet(FieldSet fieldSet) throws BindException {
        CustomPOJO result = new CustomPOJO();
        result.setName(fieldSet.readString(0));
        result.setAddress(fieldSet.readString(24));
        return result;
    }

}

For further explanation on how to use the reader, please refer to this tutorial

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!