简体   繁体   English

使用弹簧批同时处理所有生产线

[英]Process all lines at the same time with spring batch

I'm writing a batch program, using Spring batch, that reads from Db and then writes the information to a csv file.我正在编写一个批处理程序,使用 Spring 批处理,它从 Db 读取,然后将信息写入 csv 文件。 I've already used this framework before, but all the other programs that I wrote had the same structure:我之前已经使用过这个框架,但是我编写的所有其他程序都具有相同的结构:
1- read a line from db; 1- 从数据库中读取一行;
2- process the line individually; 2- 单独处理生产线;
3- write the line to the file; 3- 将行写入文件;
But this new program has a different logic: I nedd to read lines from the Db, the I have to process all of them ( ex. if I found multiple lines with the same value in field A, I need to print only one line with field B that is the sum of the field B of the other lines ), and then I have to write.但是这个新程序有一个不同的逻辑:我需要从 Db 中读取行,我必须处理所有这些行(例如,如果我在字段 A 中发现多行具有相同的值,我只需要打印一行字段 B 是其他行的字段 B 的总和),然后我必须写。 Is it possible to do something like this with Spring batch ?是否可以使用 Spring Batch 做这样的事情? ( any example that I found works on single line ). (我发现的任何示例都适用于单行)。
Thanks.谢谢。

Instead of working with chunks (readers, writer, processors), Spring Batch can also work with Tasklets .除了使用块(读取器、写入器、处理器)之外,Spring Batch 还可以与Tasklets 一起使用 A Tasklet is essentially one method, that does everything. Tasklet 本质上是一种方法,它可以做所有事情。

public class YourTasklet implements Tasklet {


    public RepeatStatus execute(StepContribution contribution,
                                ChunkContext chunkContext) throws Exception {


        // TODO do everything you want here 
        return RepeatStatus.FINISHED;
    }


}

You might want to read up on tasklets in the official documentation.您可能想阅读官方文档中的tasklet

Aggregate functions require the entire data set to be processed in order to be calculated.聚合函数需要处理整个数据集才能进行计算。 The chunk-oriented processing model is not very friendly to implementing this kind of functions since data is processed in separate chunks.面向块的处理模型对于实现这种功能不是很友好,因为数据是在单独的块中处理的。

In your case, since data is read from a database, I would leave the grouping/summing to the database with something like select sum(fieldB) ... group by fieldB .在您的情况下,由于数据是从数据库中读取的,我会将分组/求和留给数据库,例如select sum(fieldB) ... group by fieldB Databases are well optimized for this kind of tasks and you should end up reading a single record for each group.数据库针对此类任务进行了很好的优化,您最终应该为每个组读取一条记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM