读取多行以创建一条线的最佳方法是什么?

时间:2019-02-13 16:25:54

标签: java spring spring-batch chunks

我正在尝试基于一组线(从Db或CSV没关系)来计算多个事物。
例如:我有100行,每行都有一个 Delay 字段,我想获得这100行的 averageDelay 来创建一个具有该值和其他内容的行这100行(例如,发生的延迟大于一定时间)。

我早已知道,这就是为什么我需要一些最佳方法的建议,而无需编写一些肮脏的代码。我知道春天真的很强大,但是我在这里缺少一些技巧。

我的代码主要基于此example(使用大块的代码) 但是我只是在寻找一个好的结构!

假设我有一个课程 InitialLine

public class InitialLine{

    private String feedID;
    private LocalDate dateTime;
    private Integer delay;


    /* Proper getters and setters here */

}

finalLine

public class finalLine implements Serializable{

    private String feedId;
    private LocalDate dateTime;
    private Float averageDelay;
    // Delay greater than 100 second
    private Integer delayGT100;
    // Delay greater than 200 second
    private Integer delayGT200;

    /* Proper getters and setters here */

}

我的 ItemReader 看起来像这样:

public class initialLineReader extends ItemReader<List<InitialLine>>{

    /* Declaration here*/

    private FilesUtils fu;

    @Override
    public List<InitialLine> read(){
        List<InitialLine> lines = fu.readInitialLines();
         return listFlux;
    }

    /* Other stuff*/

} 

通过 FileUtils 如此运行:

public class FilesUtils {

    /* Declaration here */

    /* Here we are using a CSV file but it will be connected to DB after*/
    public List<InitialLine> readInitialLines() {
        try {
            if (CSVReader == null) initReader();


            // What should I put here ? 
            // Read every line with .readNext() and stacking it inside a List<InitialLine> ?


            String[] line = CSVReader.readNext();
            if (line == null) return null;
            return new LineFluxCapacity();
        } catch (Exception e) {
            logger.error("Error while reading line in file: " + this.fileName);
            return null;
        }
    }

    /* Other stuff here*/
}

我认为通常大块都是这样的:
-读者:阅读一个
-处理器:将 ONE 项转换为 ONE
-作家:一次写多个

但是我们可以实现一些干净的工作吗?
-读者:阅读多个项/行
-处理器:将 MULTI 个项目/行转换为一个
-作家:写 ONE

0 个答案:

没有答案
相关问题