作为批处理 API (JSR-352) 的新手,我在建模以下(简化的)场景时遇到一些困难:
- 假设我们有一个
Batchlet
在第一个step
中生成一组动态文件. - 马上
step
,所有这些文件必须在chunk
中单独处理s(通过ItemReader
、ItemProcessor
和ItemWriter
)生成一组新文件。 - 第三个
step
这些新文件需要打包到一个大存档中。
我找不到定义第二步的方法,因为规范似乎没有提供循环构造(根据我的理解, partition
、 split
和 flow
仅适用于具有已知固定尺寸)。
工作 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>
关于jsr352 - 批处理作业定义: How to run a dynamically-calculated number of partitions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58427625/