I want to create a flat file which has the below format:
Col1Name;Col2Name;Col3Name
one;23;20120912
two;28;20120712
As seen, the first line in the flat file are the column names.
How to achieve this through header callback ?
I see that if the input file is of above format, there is an option as below to ignore first line:
<property name="firstLineIsHeader" value="true"/>
Also, this Jira Issue indicates that what I want is implemeted and closed. However, I am unable to find any example for writing first line as column names.
<beans:bean id="MyFileItemWriter" class="com.nik.MyFileItemWriter" scope="step">
<beans:property name="delegate">
<beans:bean class="org.springframework.batch.item.file.FlatFileItemWriter">
<beans:property name="resource" value="file:MYFILE.dat" />
<beans:property name="lineAggregator">
<beans:bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<beans:property name="delimiter" value=";" />
<beans:property name="fieldExtractor">
<beans:bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<beans:property name="names" value="Col1Name, Col2Name, Col3Name" />
</beans:bean>
</beans:property>
</beans:bean>
</beans:property>
<beans:property name="headerCallback" ref="MyFileItemWriter" />
</beans:bean>
</beans:property>
</beans:bean>
My Item Writer looks as below:
public class MyFileItemWriter implements ItemWriter<MyBean>, FlatFileHeaderCallback, ItemStream{
private FlatFileItemWriter<MyBean> delegate;
public void setDelegate(final FlatFileItemWriter<MyBean> delegate) {
this.delegate = delegate;
}
public void writeHeader(Writer writer) throws IOException {
}
public void write(List<? extends MyBean> items) throws Exception {
this.delegate.write(items);
}
public void close() throws ItemStreamException {
this.delegate.close();
}
public void open(ExecutionContext arg0) throws ItemStreamException {
this.delegate.open(arg0);
}
public void update(ExecutionContext arg0) throws ItemStreamException {
this.delegate.update(arg0);
}
}
Thanks for reading!
ItemWriter. It is the element of the step of a batch process which writes data. An ItemWriter writes one item a time. Spring Batch provides an Interface ItemWriter. All the writers implement this interface.
Interface used to create string representing object.
Class BeanWrapperFieldExtractor<T> This is a field extractor for a java bean. Given an array of property names, it will reflectively call getters on the item and return an array of all the values.
ItemWriter defines the batch artifact that writes to a list of items for chunk processing.
create a custom class which extends the FlatFileItemWriter and implements just the constructor:
public class MyFlatFileWriter extends FlatFileItemWriter {
public MyFlatFileWriter (){
super.setHeaderCallback(new FlatFileHeaderCallback() {
public void writeHeader(Writer writer) throws IOException {
writer.write("Col1Name,Col2Name,Col3Name");
}
});
}
and then use this class in the bean configuration class attribute
well did you try to work with
public void writeHeader(Writer writer) throws IOException {
//... e.g. writer.write("my first line");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With