首页 文章

Spring批量读取器文件

提问于
浏览
1

我正在使用spring boot和spring批处理框架开发Spring webapp .

我们有一组复杂的和不同的json文件,我们需要:

  • 读取每个文件

  • 略微修改其内容

  • 最后将它们存储在mongodb中 .

问题:使用spring batch执行此任务是否合理?正如我在教程示例等中看到的那样,spring批处理是逐行处理的正确工具,但是逐个文件呢?

我没有写作者(MongoItemWritter)和处理器的问题,但我没有看到如何实现阅读器 .

谢谢!

1 回答

  • 2

    yes 你可以绝对使用Spring Batch . Reader的项目可以是 File .

    public class CustomItemReader implements InitializingBean{
    
        private List<File> yourFiles= null;
    
        public File read() {
            if ((yourFiles!= null) && (yourFiles.size() != 0)) {
                return yourFiles.remove(0);
            }
            return null;
        }
    
        //Reading Items from Service
        private void reloadItems() {
            this.yourItems= new ArrayList<File>();
           // populate the items
         }
    
    
        @Override
        public void afterPropertiesSet() throws Exception {
            reloadItems();
        }
    }
    

    自定义处理器:

    public class MyProcessor implements ItemProcessor<File, File> {
    @Override
    public File process(File arg0) throws Exception {
       // Apply any logic to your File before transferring it to the writer
     return arg0;
     }
    }
    

    和一个自定义作家:

    public class MyWriter{
          public void write(File file) throws IOException {
             }
       }
    

相关问题