Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用SpringBatch读取将输入CSV文件中的行转换为具有1对多关系的输出CSV文件_Java_Spring Batch - Fatal编程技术网

Java 使用SpringBatch读取将输入CSV文件中的行转换为具有1对多关系的输出CSV文件

Java 使用SpringBatch读取将输入CSV文件中的行转换为具有1对多关系的输出CSV文件,java,spring-batch,Java,Spring Batch,我已经发表了这个问题: 并回顾了这些相关答案: 但仍然不知道如何使用Spring Batch来: 从输入CSV文件中读取一行 处理它并生成一个或多个输出行 将输出行写入输出文件 我知道解决方案应该是实现一个writer,该writer将接受项目列表,并以某种方式使用“委托”,以便逐个处理项目 如果有人能解释一下,我将不胜感激 我的代码: public class CsvRowsProcessor implements ItemProcessor<RowInput, List<Ro

我已经发表了这个问题:

并回顾了这些相关答案:

但仍然不知道如何使用Spring Batch来:

  • 从输入CSV文件中读取一行
  • 处理它并生成一个或多个输出行
  • 将输出行写入输出文件
  • 我知道解决方案应该是实现一个writer,该writer将接受项目列表,并以某种方式使用“委托”,以便逐个处理项目

    如果有人能解释一下,我将不胜感激

    我的代码:

    public class CsvRowsProcessor implements ItemProcessor<RowInput, List<RowOutput>>{
    
        @Override
        public List<RowOutput> process(final RowInput rowInput)  {
    
            final String id = rowInput.getId();
            final String title = rowInput.getTitle();
            final String description = rowInput.getDescription();
            final RowOutput transformedRowInput = new RowOutput(id, title, description);
    
            List<RowOutput> rows=new LinkedList<>();
            rows.add(transformedRowInput);
            return rows;
        }
    
    }
    
    @Bean
    ItemWriter<RowOutput> csvRowsWriter() {
        FlatFileItemWriter<RowOutput> csvFileWriter = new FlatFileItemWriter<>();
        csvFileWriter.setResource(new FileSystemResource("C:\\Users\\orenl\\IdeaProjects\\Spring-Batch-CSV-Example\\src\\main\\resources\\outputFile.csv"));
        LineAggregator<RowOutput> lineAggregator = createLineAggregator();
        csvFileWriter.setLineAggregator(lineAggregator);
        csvFileWriter.setHeaderCallback(new FlatFileHeaderCallback() {
    
            public void writeHeader(Writer writer) throws IOException {
                writer.write("Id,Title,Description");
            }
        });
        return csvFileWriter;
    }
    
    
    
    private LineAggregator<RowOutput> createLineAggregator() {
        DelimitedLineAggregator<RowOutput> lineAggregator = new DelimitedLineAggregator<>();
        lineAggregator.setDelimiter(",");
    
        FieldExtractor<RowOutput> fieldExtractor = createFieldExtractor();
        lineAggregator.setFieldExtractor(fieldExtractor);
    
        return lineAggregator;
    }
    
    private FieldExtractor<RowOutput> createFieldExtractor() {
        BeanWrapperFieldExtractor<RowOutput> extractor = new BeanWrapperFieldExtractor<>();
        extractor.setNames(new String[] { "Id", "Title", "Description" });
        return extractor;
    }
    
    @Bean
    public Step csvFileToFileStep() {
        return stepBuilderFactory.get("csvFileToFileStep")
                .<RowInput ,RowOutput>chunk(1)
                .reader(csvRowsReader())
                .processor(csvRowsProcessor())
                .writer(csvRowsWriter())
                .build();
    }
    
    @Bean
    Job csvFileToCsvJob(JobCompletionNotificationListener listener) {
        return jobBuilderFactory.get("csvFileToCsvJob")
                .incrementer(new RunIdIncrementer())
                .listener(listener)
                .flow(csvFileToFileStep())
                .end()
                .build();
    }
    
    公共类CsvRowsProcessor实现ItemProcessor{
    @凌驾
    公共列表过程(最终行输入行输入){
    最终字符串id=rowInput.getId();
    最后一个字符串title=rowInput.getTitle();
    最终字符串描述=rowInput.getDescription();
    最终行输出transformedRowInput=新行输出(id、标题、描述);
    列表行=新建LinkedList();
    添加(transformedRowInput);
    返回行;
    }
    }
    @豆子
    ItemWriter csvRowsWriter(){
    FlatFileItemWriter csvFileWriter=新FlatFileItemWriter();
    setResource(新的文件系统资源(“C:\\Users\\orenl\\IdeaProjects\\Spring批处理CSV示例\\src\\main\\resources\\outputFile.CSV”);
    LineAggregator LineAggregator=createLineAggregator();
    csvFileWriter.setLineAggregator(lineAggregator);
    csvFileWriter.setHeaderCallback(新的FlatFileHeaderCallback(){
    public void writeHeader(Writer-Writer)引发IOException{
    写信人(“身份证、头衔、描述”);
    }
    });
    返回csvFileWriter;
    }
    专用LineAggregator createLineAggregator(){
    DelimitedLineAggregator lineAggregator=新的DelimitedLineAggregator();
    lineAggregator.setDelimiter(“,”);
    FieldExtractor FieldExtractor=createFieldExtractor();
    lineAggregator.setFieldExtractor(fieldExtractor);
    回流管集热器;
    }
    私有FieldExtractor createFieldExtractor(){
    BeanWrapperFieldExtractor=新的BeanWrapperFieldExtractor();
    setNames(新字符串[]{“Id”、“Title”、“Description”});
    回流提取器;
    }
    @豆子
    公共步骤csvFileToFileStep(){
    返回stepBuilderFactory.get(“csvFileToFileStep”)
    .chunk(1)
    .reader(csvRowsReader())
    .processor(csvRowsProcessor())
    .writer(csvRowsWriter())
    .build();
    }
    @豆子
    作业csvFileToCsvJob(作业完成通知侦听器侦听器){
    返回jobBuilderFactory.get(“csvFileToCsvJob”)
    .incrementer(新的RunIdIncrementer())
    .listener(侦听器)
    .flow(csvFileToFileStep())
    (完)
    .build();
    }
    
    以下是一个示例:

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    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.ItemProcessor;
    import org.springframework.batch.item.ItemReader;
    import org.springframework.batch.item.ItemWriter;
    import org.springframework.batch.item.support.ListItemReader;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @EnableBatchProcessing
    public class MyJob {
    
        @Autowired
        private JobBuilderFactory jobs;
    
        @Autowired
        private StepBuilderFactory steps;
    
        @Bean
        public ItemReader<Integer> itemReader() {
            return new ListItemReader<>(Arrays.asList(1, 3, 5, 7, 9));
        }
    
        @Bean
        public ItemProcessor<Integer, List<Integer>> itemProcessor() {
            return item -> {
                List<Integer> result = new ArrayList<>();
                result.add(item);
                result.add(item + 1);
                return result;
            };
        }
    
        @Bean
        public ItemWriter<List<Integer>> itemWriter() {
            return items -> {
                for (List<Integer> item : items) {
                    for (Integer integer : item) {
                        System.out.println("integer = " + integer);
                    }
                }
            };
        }
    
        @Bean
        public Step step() {
            return steps.get("step")
                    .<Integer, List<Integer>>chunk(2)
                    .reader(itemReader())
                    .processor(itemProcessor())
                    .writer(itemWriter())
                    .build();
        }
    
        @Bean
        public Job job() {
            return jobs.get("job")
                    .start(step())
                    .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());
        }
    
    }
    
    您可以调整示例以读取/写入文件


    希望这能有所帮助。

    非常感谢马哈茂德,非常感谢!很高兴它有帮助!
    integer = 1
    integer = 2
    integer = 3
    integer = 4
    integer = 5
    integer = 6
    integer = 7
    integer = 8
    integer = 9
    integer = 10