[英]spring batch write in some files with different names
I'm new in Spring-Batch world and I'm triying to do the following: 我是Spring-Batch领域的新手,现在正在努力做到以下几点:
Read from a file that have inside something like this: 从文件中读取内容如下:
65198;4 65257;9 66745;14 67091;3 64206;10 112233;8
and one of those codes doesn't exist. 这些代码之一不存在。
My goal is that write in a file with the name "Exist.txt" the codes that exist and in other file with the name "NoExist.txt" the other ones. 我的目标是在名称为“ Exist.txt”的文件中写入存在的代码,在名称为“ NoExist.txt”的其他文件中写入其他代码。
I have only one processor that do this work. 我只有一个处理器可以完成这项工作。 Here the processor
这里的处理器
@Bean
@StepScope
public ItemProcessor<HanaskaAssitedRequestSendedFileVO,AssitedRequestFileVO> processor(IHorecaAssistedRequestProcessorGestor horecaAssistedRequestProcessorGestor){
return new ItemProcessor<HanaskaAssitedRequestSendedFileVO,AssitedRequestFileVO>() {
@Override
public AssitedRequestFileVO process(HanaskaAssitedRequestSendedFileVO item) throws Exception {
AssitedRequestFileVO assitedRequestFileVO = new AssitedRequestFileVO();
Set<String> itemsBarCode = new HashSet<>();
BusquedaArticulosRequestVO busquedaArticulosRequestVO = new BusquedaArticulosRequestVO();
return horecaAssistedRequestProcessorGestor.getDataToWrite(item,assitedRequestFileVO, itemsBarCode,
busquedaArticulosRequestVO);
}
};
}
and her is the gestor that returns data to write in a file 她是返回数据以写入文件的手势
@Override
public AssitedRequestFileVO getDataToWrite(HanaskaAssitedRequestSendedFileVO item,
AssitedRequestFileVO assitedRequestFileVO, Set<String> itemsBarCode,
BusquedaArticulosRequestVO busquedaArticulosRequestVO) {
this.validateData(busquedaArticulosRequestVO, item, itemsBarCode, assitedRequestFileVO);
return assitedRequestFileVO;
}
private void validateData(BusquedaArticulosRequestVO busquedaArticulosRequestVO,
HanaskaAssitedRequestSendedFileVO item, Set<String> itemsBarCode,
AssitedRequestFileVO assitedRequestFileVO) {
try {
this.setDataToBusquedaArticulosRequestVO(busquedaArticulosRequestVO, item, itemsBarCode);
Map<String, ArticuloVentaVO> mapItem = horecaAssistedRequestSpirngBatchService
.getDataItem(busquedaArticulosRequestVO).getMapArticuloVentaVO();
Optional<Entry<String, ArticuloVentaVO>> optItem = mapItem.entrySet().stream().findAny();
ArticuloVentaVO articuloVentaVO = null;
if (optItem.isPresent()) {
articuloVentaVO = optItem.get().getValue();
assitedRequestFileVO.setItemCode(this.addDigitsToData(articuloVentaVO.getCodigoBarras(),12));
assitedRequestFileVO.setItemPresent(true);
assitedRequestFileVO.setMeasureUnit(this.addDigitsToData(articuloVentaVO.getUnidadManejoVenta().toString(),3));
assitedRequestFileVO.setRequestedQuantity(this.addDigitsToData(item.getCantidadPedida(),3));
assitedRequestFileVO.setStoreCode("711");
assitedRequestFileVO.setStoreCode("096");
}
} catch (Exception e) {
assitedRequestFileVO.setItemCode(item.getCodigoBarras());
assitedRequestFileVO.setItemPresent(false);
logger.info("Error->"+e.getMessage());
}
}
The code above return if a code exists or not. 上面的代码返回是否存在代码。
So how can I write two distincts files with different names, filtering and writing the codes that exists or not in their appropriate file in java-config? 那么,如何才能用不同的名称编写两个不同的文件,过滤并编写在java-config的相应文件中存在或不存在的代码?
Thanks in advance!!! 提前致谢!!!
You need to use the ClassifierCompositeItemWriter in order to classify items and write each type in its corresponding file. 您需要使用ClassifierCompositeItemWriter来对项目进行分类,并将每种类型写入其对应的文件中。
Here is a self contained example you can try: 这是一个独立的示例,您可以尝试:
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 + '\'' +
'}';
}
}
}
This sample reads some Person
items and writes those with name foo*
to foos.txt
and those with name bar*
to bars.txt
. 此示例读取一些
Person
项目,并将名称为foo*
项目写入foos.txt
,将名称为bar*
项目写入bars.txt
。 You can do the same with Exist.txt
and NoExist.txt
in your case. 您可以对
Exist.txt
和NoExist.txt
进行相同的处理。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.