- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了 3 个文件编写器,它们创建了 3 个本地文件。但读取的所有信息都以相同的方式进入每个文件。我正在尝试设置一些逻辑,以便只有被调用的项目才会进入其正确的文件。例如:创建文件A、文件B、文件C。当读者访问 MONGODB 时,它应该解析 buisness 列并找到 3 种不同的类型:TypeA、TypeB 和 TypeC。一旦读者找到不同的类型,就应该将其添加到适当的文件中,而不是将所有内容打印到创建的每个文件中。 FileA 应该只包含 TypeA 的信息,除此之外不应写入任何其他信息。有办法做到吗?这是我的代码的样子。
项目阅读器:
@Bean
public MongoItemReader<PaymentAudit> mongoreader() {
LOG.debug("Mongo-Reader");
@SuppressWarnings("unchecked")
MongoItemReader<PaymentAudit> mongoreader = new MongoItemReader();
mongoreader.setTemplate(mongoTemplate);
mongoreader.setQuery("{}");
mongoreader.setTargetType(PaymentAudit.class);
mongoreader.setSort(new HashMap<String, Sort.Direction>() {
{
put("_id", Direction.ASC);
}
});
return mongoreader;
}
文件项编写器:
@StepScope
@Bean
public FlatFileItemWriter<PaymentAudit> writer() {
LOG.debug("Mongo-writer");
String exportFilePath="C:\\filewriter\\retail.txt";
FlatFileItemWriter<PaymentAudit> flatFile = new
FlatFileItemWriterBuilder<PaymentAudit>()
.name("")
.resource(new FileSystemResource(exportFilePath))
.lineAggregator(createPaymentPortalLineAggregator())
.build();
String exportFileHeader = "TypeA";
StringHeaderWriter headerWriter = new
StringHeaderWriter(exportFileHeader);
flatFile.setHeaderCallback(headerWriter);
return flatFile;
}
@Bean
public FlatFileItemWriter<PaymentAudit> writer2() {
LOG.debug("flatFileItemWriter");
String exportFilePath="C:\\filewriter\\hcc.txt";
FlatFileItemWriter<PaymentAudit> flatFile = new
FlatFileItemWriterBuilder<PaymentAudit>()
.name("")
.resource(new FileSystemResource(exportFilePath))
.lineAggregator(createPaymentPortalLineAggregator())
.build();
String exportFileHeader = "TypeB";
StringHeaderWriter headerWriter = new
StringHeaderWriter(exportFileHeader);
flatFile.setHeaderCallback(headerWriter);
return flatFile;
}
@Bean
public FlatFileItemWriter<PaymentAudit> writer3() {
LOG.debug("Mongo-writer");
String exportFilePath="C:\\filewriter\\srx.txt";
FlatFileItemWriter<PaymentAudit> flatFile = new
FlatFileItemWriterBuilder<PaymentAudit>()
.name("")
.resource(new FileSystemResource(exportFilePath))
.lineAggregator(createPaymentPortalLineAggregator())
.build();
String exportFileHeader = "TypeC";
StringHeaderWriter headerWriter = new
StringHeaderWriter(exportFileHeader);
flatFile.setHeaderCallback(headerWriter);
return flatFile;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public CompositeItemWriter<PaymentAudit> compositeItemWriter(){
CompositeItemWriter writer = new CompositeItemWriter();
private LineAggregator<PaymentAudit> createPaymentPortalLineAggregator() {
DelimitedLineAggregator<PaymentAudit> lineAggregator = new
DelimitedLineAggregator<>();
lineAggregator.setDelimiter("|");
FieldExtractor<PaymentAudit> fieldExtractor =
createPaymentPortalFieldExtractor();
lineAggregator.setFieldExtractor(fieldExtractor);
return lineAggregator;
}
private FieldExtractor<PaymentAudit> createPaymentPortalFieldExtractor() {
BeanWrapperFieldExtractor<PaymentAudit> extractor = new
BeanWrapperFieldExtractor<>();
extractor.setNames(new String[] { "TypeA, TypeB, TypeC"});
return extractor;
}
最佳答案
您需要使用ClassifierCompositeItemWriter
为了对项目进行分类并将每种类型写入其相应的文件中。这是一个您可以尝试的简单示例:
import java.util.Arrays;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step;
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.launch.JobLauncher;
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.builder.FlatFileItemWriterBuilder;
import org.springframework.batch.item.file.transform.PassThroughLineAggregator;
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 MyJob {
private JobBuilderFactory jobBuilderFactory;
private StepBuilderFactory stepBuilderFactory;
public MyJob(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
this.jobBuilderFactory = jobBuilderFactory;
this.stepBuilderFactory = stepBuilderFactory;
}
@Bean
public ItemReader<Person> itemReader() {
Person foo1 = new Person();foo1.setId(1);foo1.setName("foo1");
Person foo2 = new Person();foo2.setId(2);foo2.setName("foo2");
Person bar1 = new Person();bar1.setId(3);bar1.setName("bar1");
Person bar2 = new Person();bar2.setId(4);bar2.setName("bar2");
return new ListItemReader<>(Arrays.asList(foo1, foo2, bar1, bar2));
}
@Bean
public ClassifierCompositeItemWriter<Person> classifierCompositeItemWriter(ItemWriter<Person> fooItemWriter, ItemWriter<Person> barItemWriter) {
ClassifierCompositeItemWriter<Person> classifierCompositeItemWriter = new ClassifierCompositeItemWriter<>();
classifierCompositeItemWriter.setClassifier((Classifier<Person, ItemWriter<? super Person>>) person -> {
if (person.getName().startsWith("foo")) {
return fooItemWriter;
} else {
return barItemWriter;
}
});
return classifierCompositeItemWriter;
}
@Bean
public FlatFileItemWriter<Person> fooItemWriter() {
return new FlatFileItemWriterBuilder<Person>()
.name("fooItemWriter")
.resource(new FileSystemResource("foos.txt"))
.lineAggregator(new PassThroughLineAggregator<>())
.build();
}
@Bean
public FlatFileItemWriter<Person> barItemWriter() {
return new FlatFileItemWriterBuilder<Person>()
.name("barItemWriter")
.resource(new FileSystemResource("bars.txt"))
.lineAggregator(new PassThroughLineAggregator<>())
.build();
}
@Bean
public Step dataExtractionStep() {
return stepBuilderFactory.get("dataExtractionStep")
.<Person, Person>chunk(2)
.reader(itemReader())
.writer(classifierCompositeItemWriter(fooItemWriter(), barItemWriter()))
.stream(fooItemWriter())
.stream(barItemWriter())
.build();
}
@Bean
public Job dataExtractionJob() {
return jobBuilderFactory.get("dataExtractionJob")
.start(dataExtractionStep())
.build();
}
public static void main(String[] args) throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class);
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job job = context.getBean(Job.class);
jobLauncher.run(job, new JobParameters());
}
public static class Person {
private int id;
private String name;
public Person() {
}
public Person(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
}
此示例读取一些 Person
项目,并将名称为 foo*
的项目写入 foos.txt
以及名称为 bar* 的项目
到 bars.txt
。
希望这有帮助。
关于java - 使用 Tasklet 在 Spring Batch 中或在 CompositeItemWriter 中创建逻辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53377660/
我是 Spring Batch 的新手,目前正在使用 MultiResourceItemReader 读取多个文件,并使用 CompositeItemWriter 将其写入数据库的不同表中,每个文件包
我有以下代码 @Bean public JdbcBatchItemWriter writer1() { return new JdbcBatchItemWriterBuilder()
我需要从数据库中读取数据,然后生成一个 XLSX 文件并更新数据库中的数据。 读者部分没有问题。对于 writer 部分,我最终创建了一个 CompositeItemWriter,它由第一个生成 XL
在批处理作业步骤配置中,我计划在 writer 中执行 2 次查询,第 1 次查询是更新表 A 中的记录,然后第 2 次查询是再次在表 A 中插入新记录。 到目前为止,我认为 CompositeIte
我正在扩展这个 How does Spring Batch CompositeItemWriter manage transaction for delegate writers?这里的问题: 在我的
我创建了 3 个文件编写器,它们创建了 3 个本地文件。但读取的所有信息都以相同的方式进入每个文件。我正在尝试设置一些逻辑,以便只有被调用的项目才会进入其正确的文件。例如:创建文件A、文件B、文件C。
以下是将在作业中初始化的步骤代码。 @Bean public Step stepOne() { return stepBuilderFactory.get("stepOne")
我是一名优秀的程序员,十分优秀!