使用spring batch从db写入csv文件时,我需要根据条件在每列的分隔符前添加空格。
例如,我需要在csv文件中放置空格
Column1;Column2;Column3;Column4
AI22;FIENC234DK;EDEJJEDK;JEND4
OR2 ;JFJRN3D ;DEDERF3E;FEF
EK3R;DJE3DJJEJE;JDJENEJ ;3NEN3
我已经展示了当前正在运行的代码块
@Bean
ItemReader<CdVehicle> databaseToCsvItemReader() {
JdbcCursorItemReader<CdVehicle> databaseReader = new JdbcCursorItemReader<>();
databaseReader.setDataSource(dataSource);
databaseReader.setSql(QUERY_FIND_FRIENDS);
databaseReader.setRowMapper(new BeanPropertyRowMapper<>(CdVehicle.class));
return databaseReader;
}
@Bean
ItemWriter<CdVehicle> databaseToCsvItemWriter() {
FlatFileItemWriter<CdVehicle> csvFileWriter = new FlatFileItemWriter<>();
String exportFileHeader = "CV20VEHID;CV20RFDPR;CV20USAGE;CV20CRVEH;CV20PERID;CV20SITE;CV27PROJ;CV27TYCAI;CV27PHASE;CV27MILLE;CV27CRV;O;CV20DTECL;CV20TEINT;CV20PRINCIPALE;CV20SECONDAIRE;CV20CRBIS;CV36TYPRX";
StringHeaderWriter headerWriter = new StringHeaderWriter(exportFileHeader);
csvFileWriter.setHeaderCallback(headerWriter);
String userhome = System.getProperty("user.home");
String exportFilePath = userhome + "/tmp/cdv.txt";
csvFileWriter.setResource(new FileSystemResource(exportFilePath));
LineAggregator<CdVehicle> lineAggregator = newPersonLineAggregator();
csvFileWriter.setLineAggregator(lineAggregator);
return csvFileWriter;
}
@Bean
public Step databaseToCsvStep() {
return stepBuilderFactory.get("databaseToCsvStep")
.<CdVehicle, CdVehicle>chunk(100)
.reader(databaseToCsvItemReader())
.writer(databaseToCsvItemWriter())
.build();
}
@Bean
Job databaseToCsvJob() {
return jobBuilderFactory.get("databaseToCsvJob")
.incrementer(new RunIdIncrementer())
.flow(databaseToCsvStep())
.end()
.build();
}
private LineAggregator<CdVehicle> newPersonLineAggregator() {
DelimitedLineAggregator<CdVehicle> lineAggregator = new DelimitedLineAggregator<>();
lineAggregator.setDelimiter(";");
FieldExtractor<CdVehicle> fieldExtractor = newPersonFieldExtractor();
lineAggregator.setFieldExtractor(fieldExtractor);
return lineAggregator;
}
private FieldExtractor<CdVehicle> newPersonFieldExtractor() {
BeanWrapperFieldExtractor<CdVehicle> extractor = new BeanWrapperFieldExtractor<>();
extractor.setNames(new String[] {"CV20VEHID","CV20RFDPR","CV20USAGE","CV20CRVEH","CV20DTAPP","CV20PERID","CV20SITE","CV27PROJ","CV27TYCAI","CV27PHASE","CV27MILLE","CV27CRV","CV27TYMOT","O","CV20DTECL","CV20TEINT","CV20PRINCIPALE", "CV20SECONDAIRE","CV20CRBIS","CV36TYPRX"});
return extractor;
}
运行Spring Batch作业后,我得到的结果例如
Column1;Column2;Column3;Column4
AI22;FIENC234DK;EDEJJEDK;JEND4
OR2;JFJRN3D;DEDERF3E;FEF
EK3R;DJE3DJJEJE;JDJENEJ;3NEN3
为了达到间距如何用条件逐列检查。有谁知道如何通过列标题名称条件检查并在迭代过程中在定界符前提供空格。
答案 0 :(得分:1)
您可以通过使用Spring Batch Formatter
来利用String
FormatterLineAggregator
语法。只需将您现有的LineAggregator
实现替换为以下内容:
private LineAggregator<CdVehicle> newPersonLineAggregator() {
FormatterLineAggregator<CdVehicle> lineAggregator = new FormatterLineAggregator<>();
lineAggregator.setFieldExtractor(newPersonFieldExtractor());
lineAggregator.setFormat("%-4s;%-10s;%-8d;%-5s");
return lineAggregator;