Amuthan
Amuthan

Reputation: 111

putting count of lines into header of flat file

I'm using spring-batch to create flat file reports basing on DB data. The format of flat file is similar to one below

HEADER1 (File Name) HEADER2 (contains number of records)
HEADER3 (Column Names)
RECORD1
RECORD2
RECORDN
FOOTER
I'm using FlatFileHeaderCallback and FlatFileHeaderCallback interfaces to write header and footer accordingly. I'm able to insert the row count in FOOTER but when i am trying to write it in Header its returing 0.

FileWriter:

private String templateDelimiter;
private String fileName;
private int rowCount = 0;
private String headerTemplate;
private String footerTemplate;
private String columnHeaders;

private Map<String, String> templateProps;

private FlatFileItemWriter<String[]> delegate;    


public void writeFooter(Writer writer) throws IOException {
    if (footerTemplate != null) {
        initializeTemplateProps();
        templateProps.put(Constant.TEMPLATE_DELIMITER + Constant.ROWCOUNT + Constant.TEMPLATE_DELIMITER, "" + rowCount);
        writer.write(TextTemplate.merge(footerTemplate, templateProps));
    }
}

public void writeHeader(Writer writer) throws IOException {
    if (headerTemplate != null) {
        initializeTemplateProps();      
        writer.write(TextTemplate.merge(headerTemplate, templateProps));
    }
}

protected void initializeTemplateProps() {
    if (templateProps == null) {
        templateProps = new HashMap<String, String>();
        templateProps.put(Constant.TEMPLATE_DELIMITER + Constant.FILENAME + Constant.TEMPLATE_DELIMITER, fileName);
        templateProps.put(Constant.TEMPLATE_DELIMITER + Constant.SCHEDULE_DATE + Constant.TEMPLATE_DELIMITER, System.getProperty(Constant.SCHEDULE_DATE));
        templateProps.put(Constant.TEMPLATE_DELIMITER + Constant.COLUMNHEADER + Constant.TEMPLATE_DELIMITER, columnHeaders);
        templateProps.put(Constant.TEMPLATE_DELIMITER + Constant.NEWLINE + Constant.TEMPLATE_DELIMITER, System.lineSeparator()); 
        templateProps.put(Constant.TEMPLATE_DELIMITER + Constant.PSV_FILE_HEADER + Constant.TEMPLATE_DELIMITER + 
                Constant.TEMPLATE_DELIMITER+Constant.NEWLINE + Constant.TEMPLATE_DELIMITER+Constant.TEMPLATE_DELIMITER+Constant.COLUMNHEADER+Constant.TEMPLATE_DELIMITER,headerTemplate);
    }
    templateProps.put(Constant.TEMPLATE_DELIMITER + Constant.ROWCOUNT + Constant.TEMPLATE_DELIMITER, "" + rowCount);
}

public void write(List<? extends String[]> items) throws Exception {        
    rowCount += items.size();
    delegate.write(items);
}   

Upvotes: 0

Views: 1358

Answers (1)

Michael Pralow
Michael Pralow

Reputation: 6630

I'm able to insert the row count in FOOTER but when i am trying to write it in Header its returing 0.

at the time of the creation of the header the value is 0, because no items are read/processed/written yet

to solve the problem i would go with

  • a TaskletStep which runs a SELECT COUNT.. SQL to get the input count
  • using ExecutionContext to store the value
  • use the value in your existing step to write it into the header

another solution could be to use another chunk oriented step, which reads and writes the created file again, changing only the desired value

Upvotes: 1

Related Questions