putting count of lines into header of flat file

别来无恙 提交于 2020-02-08 02:44:08

问题


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);
}   

回答1:


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



来源:https://stackoverflow.com/questions/37458791/putting-count-of-lines-into-header-of-flat-file

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