我有一个批处理作业,它将使用输入作业参数作为任何选择查询从Web服务中调用。然后,春季批处理作业将使用JdbcCursorItemReader,然后使用FlatFileItemWriter运行该查询,以将数据写入CSV。我的问题是我无法从读取器中的元数据获取到写入器的列名(同一步骤)。
有关如何执行此操作的任何建议?注意:我试图从书写器中构建并访问标头字符串,但是bean初始化过程似乎期望标头在那时准备好。结果,标题始终为空。
@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;
}
答案 0 :(得分:0)
@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;
}