我试图从数据库读取客户端数据并将处理后的数据写入平面文件。 但是在写入数据之前我需要处理整个ItemReader
的结果。Spring批处理如何在写入数据之前处理数据列表
例如,我从数据库中读取行客户端:
public class Client {
private String id;
private String subscriptionCode;
private Boolean activated;
}
但我想算,写多少用户被激活,通过subscriptionCode分组:
public class Subscription {
private String subscriptionCode;
private Integer activatedUserCount;
}
我不知道如何执行使用ItemReader
/ItemProcessor
/ItemWriter
,你能帮助我吗?
BatchConfiguration:
@CommonsLog
@Configuration
@EnableBatchProcessing
@EnableAutoConfiguration
public class BatchConfiguration {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<Client, Client> chunk(1000)
.reader(new ListItemReader<Client>(new ArrayList<Client>() { // Just for test
{
add(Client.builder().id("1").subscriptionCode("AA").activated(true).build());
add(Client.builder().id("2").subscriptionCode("BB").activated(true).build());
add(Client.builder().id("3").subscriptionCode("AA").activated(false).build());
add(Client.builder().id("4").subscriptionCode("AA").activated(true).build());
}
}))
.processor(new ItemProcessor<Client, Client>() {
public Client process(Client item) throws Exception {
log.info(item);
return item;
}
})
.writer(new ItemWriter<Client>() {
public void write(List<? extends Client> items) throws Exception {
// Only here I can use List of Client
// How can I process this list before to fill Subscription objects ?
}
})
.build();
}
@Bean
public Job job1(Step step1) throws Exception {
return jobBuilderFactory.get("job1").incrementer(new RunIdIncrementer()).start(step1).build();
}
}
主要应用:
public class App {
public static void main(String[] args) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {
System.exit(SpringApplication.exit(SpringApplication.run(BatchConfiguration.class, args)));
}
}
不知道我是否完全理解了这个问题,你确实有'ItemProcessor'的权利..你可以在你的个人客户端实例上做任何处理 - 除了你打算执行的处理以外? – 2014-10-11 15:12:15
我想统计有多少用户已将'activated'标志设置为'true',按subscriptionCode进行分组。所以我需要一个'客户'列表来确定我的'订阅'列表。但是使用'chunk',我只能逐行处理......而不是一个组。这个例子中的结果尝试应该是一个List of 2 subscription:'Subscription(subscriptionCode = AA,activatedUserCount = 2)'和'Subscription(subscriptionCode = BB,activatedUserCount = 1)' – Aure77 2014-10-11 15:32:11