【发布时间】:2021-08-08 18:40:11
【问题描述】:
我正在使用 Spring Batch 编写多个报告。 要求是我将获得带有 BranchId 和名称的记录。我需要为每个 branchId 创建一个文件,并将相应的数据连同一些页眉和页脚写入该文件。
例子:
Student A = new Student("A",1);
Student B = new Student("B",2);
Student C = new Student("C",1);
Student D = new Student("D",4);
在这种情况下,它应该创建总共 3 个文件
file1-->1.txt(with A,C)
file2-->2.txt(with B)
file3-->4.txt(with D))
.
我正在使用 ClassifierCompositeItemWriter 根据数据(在本例中为 id)创建/重用 FlatFileItemWriter 并能够成功创建文件。 对于页眉和页脚 - 在编写器级别使用回调。 生成的文件只有 HEADER 和 DATA。但不知何故,FOOTER 根本没有被执行。
在页脚之前关闭文件或使用 STEP SCOPE 似乎存在一些问题。
有人可以帮我调用 FOOTER。
这里是代码。
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor;
import org.springframework.batch.item.file.transform.DelimitedLineAggregator;
import org.springframework.batch.item.support.ClassifierCompositeItemWriter;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.classify.Classifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
@Configuration
@EnableBatchProcessing
public class MyJob3 {
public static void main(String[] args) throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(MyJob3.class);
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job job = context.getBean(Job.class);
jobLauncher.run(job, new JobParameters());
}
@Bean
public Job job(JobBuilderFactory jobs, StepBuilderFactory steps) {
return jobs.get("job").start(steps.get("step").<Student, Student>chunk(5)
.reader(itemReader())
.writer(getStudentItemWriter(itemWriterClassifier()))
.build())
.build();
}
@Bean
@StepScope
public ItemReader<Student> itemReader() {
Student A = new Student("A", 1);
Student B = new Student("B", 2);
Student C = new Student("C", 1);
Student D = new Student("D", 4);
Student E = new Student("E", 4);
return new ListItemReader<>(Arrays.asList(A,B,C,D,E));
}
Map<Integer, FlatFileItemWriter<Student>> map = new HashMap<>();
@Bean
@StepScope
public ClassifierCompositeItemWriter<Student> getStudentItemWriter(Classifier<Student, ItemWriter<? super Student>> classifier) {
ClassifierCompositeItemWriter<Student> compositeItemWriter = new ClassifierCompositeItemWriter<>();
compositeItemWriter.setClassifier(classifier);
return compositeItemWriter;
}
@Bean
@StepScope
public Classifier<Student, ItemWriter<? super Student>> itemWriterClassifier() {
return student -> {
System.out.println("Branch Id ::" + student.getBranchId() + " and Student Name" + student.getName());
if (map.containsKey(student.getBranchId())) {
FlatFileItemWriter<Student> result = map.get(student.getBranchId());
System.out.println("Exising Writer object ::" + result);
return result;
}
String fileName ="Branch_Info_" + student.getBranchId() + ".txt";
BeanWrapperFieldExtractor<Student> fieldExtractor = new BeanWrapperFieldExtractor<>();
fieldExtractor.setNames(new String[] { "branchId", "name" });
DelimitedLineAggregator<Student> lineAggregator = new DelimitedLineAggregator<>();
lineAggregator.setFieldExtractor(fieldExtractor);
FlatFileItemWriter<Student> flatFileItemWriter = new FlatFileItemWriter<>();
flatFileItemWriter.setResource(new FileSystemResource(fileName));
flatFileItemWriter.setAppendAllowed(true);
flatFileItemWriter.setLineAggregator(lineAggregator);
System.out.println("Writing header...");
flatFileItemWriter.setHeaderCallback(writer -> writer.write("Header"));
System.out.println("Writing Footer...");
flatFileItemWriter.setFooterCallback(writer -> writer.write("Footer"));
System.out.println("Writing done...");
flatFileItemWriter.open(new ExecutionContext());
map.put(student.getBranchId(), flatFileItemWriter);
System.out.println("New Writer object ::" + flatFileItemWriter);
return flatFileItemWriter;
};
}
}
【问题讨论】:
-
问题是您自己调用
FlatFileItemWriter.open(这是不正确的),而不是调用页脚回调的.close()。您不应该自己调用这些方法。您需要将委托编写者声明为 bean 并将它们注册为您的步骤中的流。有关完整示例,请参阅重复问题。 -
@MahmoudBenHassine 谢谢。就我而言,我没有固定数量的编写器(在您的情况下为 foo 和 boo),它们将是动态的,需要在 RUN 时创建。关于如何做并将它们注册到步骤的任何建议?
-
在这种情况下,您需要使用
select distinct(id) from table之类的查询或类似机制预先计算可能的不同值(在您的情况下为 1、2 和 4),并动态创建 @987654328 @bean 并在您的步骤中将它们注册为流。 -
我添加了一个带有完整示例的答案,该示例显示了我提到的方法。
标签: java spring-batch footer compositeitemwriter