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

3

11

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!

Hazy answered 12/9, 2012 at 12:19 Comment(0)
F
6

well did you try to work with

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

  } 
Filly answered 12/9, 2012 at 22:12 Comment(3)
Yes, I could do that. But I thought that would be synonymous to hard coding. Instead, I thought there would be a way present to list down the header directly from bean property names automatically. So want to confirm that there is no such functionality available. If not, I will go with the above way.Hazy
to minimize the hard coding you could re-use the attribute name list with e.g. static.springsource.org/spring/docs/3.1.x/…Filly
Thanks! Will look into it. Could you please check out the new question posted by me - Footer Call Back getting called after AfterStep call...Hazy
K
18

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

Kalina answered 29/5, 2014 at 21:1 Comment(0)
F
6

well did you try to work with

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

  } 
Filly answered 12/9, 2012 at 22:12 Comment(3)
Yes, I could do that. But I thought that would be synonymous to hard coding. Instead, I thought there would be a way present to list down the header directly from bean property names automatically. So want to confirm that there is no such functionality available. If not, I will go with the above way.Hazy
to minimize the hard coding you could re-use the attribute name list with e.g. static.springsource.org/spring/docs/3.1.x/…Filly
Thanks! Will look into it. Could you please check out the new question posted by me - Footer Call Back getting called after AfterStep call...Hazy
F
2

FlatFileItemWriter gives you option to add a header callback

headerCallback will be called before writing the first item to file.

So you need to Implement your Header Callback by implementing FlatFileHeaderCallback

Or Use Default Implementation

Fifteenth answered 28/3, 2016 at 15:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.