批处理作业定义:如何运行一个动态计算的分区数?

Batch job definition: How to run a dynamically-calculated number of partitions?

作为批处理 API (JSR-352) 的新手,我在为以下(简化的)场景建模时遇到了一些困难:

  1. 假设我们有一个 Batchlet 在第一个 step 中生成一组动态文件。
  2. 一秒 step,所有这些文件必须在 chunk 秒内单独处理(通过 ItemReaderItemProcessorItemWriter),导致一组新文件。
  3. 在第三次 step 中,这些新文件需要打包到一个大型存档中。

我找不到定义第二步的方法,因为规范似乎没有提供循环结构(据我所知 partitionsplitflow 仅适用于已知固定大小的集合。

工作 xml 的定义是什么样子的?我是否必须在第二步中放弃分块的想法,或者我必须将任务分成多个作业?还有其他选择吗?

您可以使用 PartitionMapper 以编程方式为分区步骤定义动态分区数。

映射器需要创建一个 PartitionPlan 对象来设置分区数并为每个分区提供特定于分区的属性。

您的映射器的 mapPartitions() 方法将如下所示:

public PartitionPlan mapPartitions() throws Exception {

    int numPartitions = // calculate number of partitions, however you want

    // create an array of Properties objects, one for each partition
    Properties[] props = new Properties[numPartitions];

    for (int i = 0; i < numPartitions; i++) {
        // create a Properties object for this partition
        props[i] = new Properties();

        props[i].setProperty("abc", ...);
        props[i].setProperty("xyz", ...);
    }

    // use the built-in PartitionPlanImpl from the spec or your own impl
    PartitionPlan partitionPlan = new PartitionPlanImpl(); 
    partitionPlan.setPartitions(numPartitions);

    // cet the Properties[] onto your plan
    partitionPlan.setPartitionProperties(props);

    return partitionPlan;
}

然后您可以像这样在替换中引用特定于分区的 属性 值(这与您引用静态定义的分区属性的方式相同):

    <batchlet ref="myBatchlet">
        <properties>
            <property name="propABC" value="#{partitionPlan['abc']}" />
            <property name="propXYZ" value="#{partitionPlan['xyz']}" />
        </properties>
    </batchlet>