简体   繁体   English

用于Spring Batch Reader的多个SELECT SQL-阅读器链接

[英]Multiple SELECT SQLs for Spring Batch Reader - reader chaining

I have a requirement to execute eleven SQL SELECT queries sequentially in Spring Batch reader. 我需要在Spring Batch阅读器中顺序执行十一个SQL SELECT查询。 These SELECT SQLs do INNER JOIN on quite big tables and diff from each other by a single column name in WHERE clause. 这些SELECT SQL在很大的表上进行INNER JOIN ,并且通过WHERE子句中的单个列名彼此进行比较。

Output object type by all these readers would be same, lets say VO . 所有这些阅读器的输出对象类型都是相同的,可以说VO

So how do I achieve that? 那么我该如何实现呢?

I can pass where clause String in reader which would further set in query provider. 我可以在阅读器中传递where子句String ,该子句将在查询提供程序中进一步设置。

@Bean
public ItemReader<VO> reader(String whereClause, @Value("#{stepExecutionContext[partitionNumber]}") String partitionNumber){

}

I am not sure as how to construct Spring Batch step which sets up these eleven SQLs in readers sequentially and executes them sequentially too. 我不确定如何构造Spring Batch步骤,该步骤在阅读器中顺序设置这11条SQL,然后也顺序执行。 There would be single processor and single writer since output type of all readers is same. 由于所有读取器的输出类型相同,因此将只有一个处理器和一个写入器。

Source of all these readers would be same DB tables and I would like to use JdbcPagingItemReader due to paging functionality. 所有这些阅读器的来源都是相同的数据库表,由于分页功能,我想使用JdbcPagingItemReader

My current reader is part of a partitioned step where String partitionNumber is the partitioning criteria. 我当前的阅读器是分区步骤的一部分,其中String partitionNumber是分区标准。

What I meant to ask is , can I chain readers if their output type is common? 我要问的是,如果读者的输出类型是通用的,我可以将他们链接起来吗? I don't have a problem if these readers get kicked sequentially but can I define a step consisting of a chain of readers for a single processor and writer? 如果这些阅读器顺序启动,我没有问题,但是我可以定义一个由单个处理器和编写器的阅读器链组成的步骤吗?

Please suggest for solution or better strategies. 请提出解决方案或更好的策略。

This is an approach 这是一种方法

  1. Partitioner level This will be the top source which will drive your Reader. 分区程序级别这将是驱动您的阅读器的主要来源。 We will set Grid-Size = 1 in this case and perform the task as SyncTask (not AsyncTask). 在这种情况下,我们将设置Grid-Size = 1并将任务作为SyncTask(而非AsyncTask)执行。

Below is code snippet 下面是代码片段

<job id="exampleJob" xmlns="http://www.springframework.org/schema/batch">
              <step id="stepId">
                     <partition step="partitionerStepId" partitioner="examplePartitioner">
                            <handler grid-size="1" task-executor="syncTaskExecutor" />
                     </partition>
              </step>
       </job>

       <step id="partitionerStepId" xmlns="http://www.springframework.org/schema/batch">
              <tasklet>
                     <chunk reader="exampleReader" writer="exampleWriter" processor="exampleProcessor" commit-interval="1"/>
              </tasklet>
       </step>

public class ExamplePartitioner implements Partitioner { 公共类ExamplePartitioner实现Partitioner {

@Override
public Map<String, ExecutionContext> partition(int gridSize) {
    Map<String, ExecutionContext> queue = new HashMap<String, ExecutionContext>();

    for (int i=0; i<NUMBER_QUERIES; i++) {
        ExecutionContext ec = new ExecutionContext();
        **ec.put("sql", [your query]);**

        queue.put("item"+i, ec);
    }

    return queue;
}

} }

Note: NUMBER_QUERIES as 11 for your case. 注意:您的情况下,NUMBER_QUERIES为11。 Note: The content of query you can store in a secure place or you can keep as constants in a class. 注意:您可以将查询内容存储在安全的地方,也可以将其作为常量保留在类中。 I'm not quite sure the queries have parameters or not because you have not mentioned yet. 我不太确定查询是否有参数,因为您还没有提到。

  1. Reader 读者 在此处输入图片说明 Note: Please build 'exampleRowMapper' to map to VO objects. 注意:请构建“ exampleRowMapper”以映射到VO对象。

To summarize, the Partitioner will build a list queries and put them into ExecutionContext and transfer them via "#{stepExecutionContext['sql']} to Reader each by each by sequence. 总而言之,分区程序将构建一个列表查询,并将其放入ExecutionContext中,并通过“#{stepExecutionContext ['sql']}”将它们按顺序依次传递给Reader。

Thanks, Nghia 谢谢,Nghia

Note: I have a problem to format the Reader, that is why I put as an image. 注意:我在格式化阅读器时遇到问题,这就是为什么我将其作为图像放置。 Note: For more information, please refer link from here 注意:有关更多信息,请从此处参考链接

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

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