springbatch-ItemProcessor使用

springbatch-ItemProcessor使用

起男 208 2023-05-31

springbatch-ItemProcessor使用

对数据进行处理

@Configuration
public class ItemProcessorDemo {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job itemProcessorDemoJob(){
        return jobBuilderFactory
                .get("itemProcessorDemoJob")
                .start(itemProcessorDemoStep())
                .build();
    }

    @Bean
    public Step itemProcessorDemoStep(){
        return stepBuilderFactory
                .get("itemProcessorDemoStep")
                .<String,String>chunk(2)
                .reader(myReader())
                .processor(myProcessor1())
                .writer(System.out::println)
                .build();
    }

    @Bean
    public ItemProcessor<String,String> myProcessor1(){
        return new ItemProcessor<String, String>() {
            @Override
            public String process(String s) throws Exception {
                return s.toUpperCase();//对数据进行处理
            }
        };
    }

    @Bean
    public ItemReader<String> myReader(){
        List<String> list = Arrays.asList("aaa","bbb","ccc");
        ItemReader<String> reader = new ListItemReader<>(list);
        return reader;
    }
}

有多个处理操作

@Configuration
public class ItemProcessorDemo {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job itemProcessorDemoJob(){
        return jobBuilderFactory
                .get("itemProcessorDemoJob")
                .start(itemProcessorDemoStep())
                .build();
    }

    @Bean
    public Step itemProcessorDemoStep(){
        return stepBuilderFactory
                .get("itemProcessorDemoStep")
                .<String,String>chunk(2)
                .reader(myReader())
                .processor(compositeItemProcessor())
                .writer(System.out::println)
                .build();
    }
    
    @Bean
    public CompositeItemProcessor<String,String> compositeItemProcessor(){
        CompositeItemProcessor<String, String> processor = new CompositeItemProcessor<>();
        //设置具体的处理
        processor.setDelegates(Arrays.asList(myProcessor1(),myProcessor2()));
        return processor;
    }

    @Bean
    public ItemProcessor<String,String> myProcessor1(){
        return new ItemProcessor<String, String>() {
            @Override
            public String process(String s) throws Exception {
                return s.toUpperCase();//对数据进行处理
            }
        };
    }
    @Bean
    public ItemProcessor<String,String> myProcessor2(){
        return new ItemProcessor<String, String>() {
            @Override
            public String process(String s) throws Exception {
                if (s.equals("BBB"))
                    return null;//相当于把数据过滤
                else
                    return s;
            }
        };
    }

    @Bean
    public ItemReader<String> myReader(){
        List<String> list = Arrays.asList("aaa","bbb","ccc");
        ItemReader<String> reader = new ListItemReader<>(list);
        return reader;
    }
}