spring batch csv:向csv添加多个标题
Posted
技术标签:
【中文标题】spring batch csv:向csv添加多个标题【英文标题】:spring batch csv:Adding multiple headers to csv 【发布时间】:2020-04-23 03:31:54 【问题描述】:如何在 csv 中写入多个标头,以便第二个标头值应来自数据库
//预期输出
personId,firstName,lastName,email,age
fullname,total // this is the second header which should come the database
kaa,karthi,sa,123@,34
//下面是spring批量写入csv的代码sn-p
@Bean(destroyMethod="")
public JdbcCursorItemReader<Person> reader()
JdbcCursorItemReader<Person> cursorItemReader = new JdbcCursorItemReader<>();
cursorItemReader.setDataSource(dataSource);
cursorItemReader.setSql("SELECT person_id,first_name,last_name,email,age FROM springbatchdb.person");
cursorItemReader.setRowMapper(new PersonRowMapper());
return cursorItemReader;
@Bean(destroyMethod="")
public PersonItenProcessor processor()
return new PersonItenProcessor();
@Bean(destroyMethod="")
public FlatFileItemWriter<Person> writer()
FlatFileItemWriter<Person> writer = new FlatFileItemWriter<Person>();
String exportFileHeader = "personId,firstName,lastName,email,age";
StringHeaderWriter headerWriter = new StringHeaderWriter(exportFileHeader);
writer.setHeaderCallback(headerWriter);
writer.setResource(new ClassPathResource("person.csv"));
DelimitedLineAggregator<Person> lineAggregator = new DelimitedLineAggregator<Person>();
lineAggregator.setDelimiter(",");
BeanWrapperFieldExtractor<Person> fieldExtractor = new BeanWrapperFieldExtractor<Person>();
fieldExtractor.setNames(new String[]"personId","firstName","lastName","email","age");
lineAggregator.setFieldExtractor(fieldExtractor);
writer.setLineAggregator(lineAggregator);
return writer;
【问题讨论】:
【参考方案1】:您可以在FlatFileHeaderCallback
中执行查询并将结果附加到标题的第一行。
【讨论】:
以上是关于spring batch csv:向csv添加多个标题的主要内容,如果未能解决你的问题,请参考以下文章
使用spring批处理和csv文件格式获取空指针异常向db发送数据
Spring-batch - 比较来自 csv 的数据并相应地更新 Salesforce 中的记录