如何将数据传递给FlatFileItemWriter

我有一个批处理作业,将从具有输入作业参数的Web服务调用任何选择查询 . 然后,spring批处理作业使用JdbcCursorItemReader运行该查询,然后使用FlatFileItemWriter将数据写入CSV . 我的问题是我无法从读者可用的元数据中获取列名到作者(同一步骤) .

有关如何做到这一点的任何建议?注意:我试图从编写器中构建和访问头字符串,但bean初始化过程似乎期望在那时准备好头 . 结果, Headers 始终为空 .

@Bean
@StepScope
public JdbcCursorItemReader<Map<String, ?>> getRowsOfDataForExportFromTable(){
JdbcCursorItemReader<Map<String, ? extends Object>> databaseReader = new JdbcCursorItemReader<>();

databaseReader.setDataSource(jdbcTemplate.getDataSource());
databaseReader.setSql("select * from SOME_TABLE where last_updated_date < DATE_SUB(NOW(), INTERVAL 10 DAY);");
databaseReader.setRowMapper(new RowMapper<Map<String, ? extends Object>>() {
    @Override
    public Map<String, ? extends Object> mapRow(ResultSet resultSet, int i) throws SQLException {
        Map<String,String> resultMap = new LinkedHashMap<>();
        int numOfColumns = resultSet.getMetaData().getColumnCount();
        for (int j = 1; j < numOfColumns+1; j++){
            String columnName = resultSet.getMetaData().getColumnName(j);
            String value = resultSet.getString(j);
            resultMap.put(columnName,value);
        }

        return resultMap;
    }
});
return databaseReader;
}


@Bean
@StepScope
public FlatFileItemWriter<Map<String,Object>> saveDBRecordsToFileSystem(){
    FlatFileItemWriter<Map<String,Object>> writer = new FlatFileItemWriter<>();
    writer.setResource(new FileSystemResource("/tmp/output.csv"));
    DelimitedLineAggregator<Map<String,Object>> delLineAgg = new DelimitedLineAggregator<>();
    delLineAgg.setDelimiter("\t");
    writer.setHeaderCallback(new FlatFileHeaderCallback() {

        public void writeHeader(Writer writer) throws IOException {
            // need to get columnsFromDatabaseTable from ItemReader???
            for (String header : columnsFromDatabaseTable) {
                writer.write(header);
            }
        }
    });
    writer.setLineAggregator(delLineAgg);
    return writer;
}

回答(1)

2 years ago

@Component
@StepScope
public class ColumnNames {

private Set<String> columnNames;

private boolean isSetOnce;

public void setColumnNames(Set<String> columnNames) {
    this.columnNames = columnNames;
    isSetOnce = true;
}

public Set<String> getColumnNames() {
    return columnNames;
}

public boolean isSetOnce() {
    return isSetOnce;
}

}


@Bean
@StepScope
public JdbcCursorItemReader<Map<String, ?>> getRowsOfDataForExportFromTable(ColumnNames columnNames) {
    JdbcCursorItemReader<Map<String, ? extends Object>> databaseReader = new JdbcCursorItemReader<>();
    databaseReader.setDataSource(jdbcTemplate.getDataSource());
    databaseReader.setSql("select * from SOME_TABLE where last_updated_date < DATE_SUB(NOW(), INTERVAL 10 DAY);");
    databaseReader.setRowMapper(new RowMapper<Map<String, ? extends Object>>() {

        @Override
        public Map<String, ? extends Object> mapRow(ResultSet resultSet, int i) throws SQLException {
            Map<String, String> resultMap = new LinkedHashMap<>();
            int numOfColumns = resultSet.getMetaData().getColumnCount();
            for (int j = 1; j < numOfColumns + 1; j++) {
                String columnName = resultSet.getMetaData().getColumnName(j);
                String value = resultSet.getString(j);
                resultMap.put(columnName, value);
            }
            if (columnNames.isSetOnce()) {
                columnNames.setColumnNames(resultMap.keySet());
            }

            return resultMap;
        }
    });
    return databaseReader;
}


@Bean
@StepScope
public FlatFileItemWriter<Map<String, Object>> saveDBRecordsToFileSystem(ColumnNames columnNames) {
    FlatFileItemWriter<Map<String, Object>> writer = new FlatFileItemWriter<>();
    writer.setResource(new FileSystemResource("tmp/output.csv"));
    DelimitedLineAggregator<Map<String, Object>> delLineAgg = new DelimitedLineAggregator<>();
    delLineAgg.setDelimiter("\t");
    writer.setHeaderCallback(new FlatFileHeaderCallback() {

        public void writeHeader(Writer writer) throws IOException {
            for (String header : columnNames.getColumnNames()) {
                writer.write(header);
            }
        }
    });
    writer.setLineAggregator(delLineAgg);
    return writer;
}