我是spring的新手,可以将数据库拆分为3个csv文件。
我的实际代码:(我不使用xml,也不想)
我尝试了很多解决方案,但没有任何效果。
class MyClassifier {
@Classifier
public String classify(Pojo Pojo) {
return String.valueOf(Pojo.getYear());
}
}
@Bean(name = "PojoWriter")
@StepScope
public ItemWriter<Pojo> itemWriter() throws Exception {
BackToBackPatternClassifier classifier = new BackToBackPatternClassifier();
classifier.setRouterDelegate(new MyClassifier());
classifier.setMatcherMap(new HashMap<String, FlatFileItemWriter<? super Pojo>>() {
FlatFileItemWriter<Pojo> writerH;
{
writerH = writer("2017",null,null); put("2017", writerH); writerH.close();
writerH = writer("2018",null,null); put("2018", writerH); writerH.close();
writerH = writer("2019",null,null); put("2019", writerH); writerH.close();
}
});
ClassifierCompositeItemWriter<Pojo> writer = new ClassifierCompositeItemWriter<Pojo>();
writer.setClassifier(classifier);
return writer;
}
我用年份分类,并且创建了3个文件
// @Bean(name = "FlatWriter")
// @StepScope
public FlatFileItemWriter<Pojo> writer( final String name,
@Value("#{stepExecutionContext['fromId']}") final String fromId,
@Value("#{stepExecutionContext['toId']}") final String toId) throws Exception {
FlatFileItemWriter<Pojo> writer = new FlatFileItemWriter<Pojo>();
writer.setResource(new FileSystemResource(name + ".csv"));
writer.setShouldDeleteIfExists(true);
writer.open(new ExecutionContext());
writer.setHeaderCallback(new FlatFileHeaderCallback() {
@Override
public void writeHeader(Writer writer) throws IOException {
writer.write(
"Id, number, name");
}
});
writer.setLineAggregator(new DelimitedLineAggregator<Pojo>() {
{
setDelimiter(",");
setFieldExtractor(new BeanWrapperFieldExtractor<Pojo>() {
{
setNames(new String[] { "Id", "number", "name" });
}
});
}
});
return writer;
}
@Bean
@StepScope
public Step step1() throws Exception {
return stepBuilderFactory.get("step1").<Foo, Pojo>chunk(10)
.reader(reader())
.processor(processor())
.writer(itemWriter())
.build();
}
@Bean
public Job exportUserJob() throws Exception {
return jobBuilderFactory.get("exportUserJob")
.flow(step1()).end().build();
}
参数:
@Component
@EnableBatchProcessing
@Configurable
和spring.batch.job.enabled=false
错误的堆栈跟踪:
2019-07-15 11:13:44.666错误12632-[[Spring Shell] o.s.batch.core.step.AbstractStep:在作业exportUserJob中执行步骤step1时遇到错误
org.springframework.batch.item.WriterNotOpenException:必须先打开Writer,然后才能将其写入 在org.springframework.batch.item.file.FlatFileItemWriter.write(FlatFileItemWriter.java:255)〜[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.item.support.ClassifierCompositeItemWriter.write(ClassifierCompositeItemWriter.java:66)〜[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.core.step.item.SimpleChunkProcessor.writeItems(SimpleChunkProcessor.java:175)〜[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.core.step.item.SimpleChunkProcessor.doWrite(SimpleChunkProcessor.java:151)〜[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.core.step.item.SimpleChunkProcessor.write(SimpleChunkProcessor.java:274)〜[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:199)〜[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:75)〜[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.core.step.tasklet.TaskletStep $ ChunkTransactionCallback.doInTransaction(TaskletStep.java:406)〜[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.core.step.tasklet.TaskletStep $ ChunkTransactionCallback.doInTransaction(TaskletStep.java:330)〜[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)〜[spring-tx-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在org.springframework.batch.core.step.tasklet.TaskletStep $ 2.doInChunkContext(TaskletStep.java:271)〜[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81)〜[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374)〜[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)〜[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)〜[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257)〜[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200)〜[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)上[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)上[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306)[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.batch.core.launch.support.SimpleJobLauncher $ 1.run(SimpleJobLauncher.java:135)[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)上[spring-core-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)〜[na:1.8.0_161] 在sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)〜[na:1.8.0_161] 在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)〜[na:1.8.0_161] 在java.lang.reflect.Method.invoke(未知来源)〜[na:1.8.0_161] 在org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration $ PassthruAdvice.invoke(SimpleBatchConfiguration.java:127)[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE] 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)上[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)[spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在com.sun.proxy。$ Proxy80.run(未知来源)[na:na] 在com.viviane.commands.CsvGenerator.transactionalCsvBuilder(CsvGenerator.java:700)[classes /:na] 在com.viviane.commands.Scanner.createFiles(Scanner.java:93)[classes /:na] 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)〜[na:1.8.0_161] 在sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)〜[na:1.8.0_161] 在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)〜[na:1.8.0_161] 在java.lang.reflect.Method.invoke(未知来源)〜[na:1.8.0_161] 在org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:216)[spring-core-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在org.springframework.shell.core.SimpleExecutionStrategy.invoke(SimpleExecutionStrategy.java:64)上[spring-shell-1.1.0.RELEASE.jar:1.1.0.RELEASE] 在org.springframework.shell.core.SimpleExecutionStrategy.execute(SimpleExecutionStrategy.java:57)[spring-shell-1.1.0.RELEASE.jar:1.1.0.RELEASE] 在org.springframework.shell.core.AbstractShell.executeCommand(AbstractShell.java:127)上[spring-shell-1.1.0.RELEASE.jar:1.1.0.RELEASE] 在org.springframework.shell.core.JLineShell.promptLoop(JLineShell.java:533)[spring-shell-1.1.0.RELEASE.jar:1.1.0.RELEASE] 在org.springframework.shell.core.JLineShell.run(JLineShell.java:179)[spring-shell-1.1.0.RELEASE.jar:1.1.0.RELEASE] 在java.lang.Thread.run(未知来源)[na:1.8.0_161]