Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring batch: Writing column names as first line in flat file

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!

like image 309
Vicky Avatar asked Sep 12 '12 12:09

Vicky


People also ask

What is ItemWriter in Spring Batch?

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.

What is Lineaggregator?

Interface used to create string representing object.

What is BeanWrapperFieldExtractor?

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.

What is ItemWriter in java?

ItemWriter defines the batch artifact that writes to a list of items for chunk processing.


2 Answers

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

like image 138
Salvatore Pannozzo Capodiferro Avatar answered Oct 20 '22 01:10

Salvatore Pannozzo Capodiferro


well did you try to work with

  public void writeHeader(Writer writer) throws IOException {
      //... e.g. writer.write("my first line");

  } 
like image 32
Michael Pralow Avatar answered Oct 20 '22 00:10

Michael Pralow