在 WebSphere JSR 352 中动态添加分区
adding partitions dynamically in WebSphere JSR 352
我的源 table 有 10 条记录,我需要读取 10 条记录并需要处理这些记录,并且需要将处理过的记录写入目标 table。对于这 10 条记录,我创建了 2 个分区。但是在我的真实项目中,我们不知道我每天需要处理多少条记录,所以我如何决定我需要多少个分区?
是否可以动态添加分区?
请在 .
找到我的 POC 代码
使用 PartitionMapper 动态定义分区步骤中的分区数。
这在步骤的开头运行并构建一个 PartitionPlan,它定义分区的数量和每个分区的属性。
在 XML 中,您的 <partition>
元素应包含子 <mapper>
元素而不是您用来静态定义分区数的 <plan>
子元素。
但是您使用 partitionPlan 属性 替换类似地执行替换。
例如
<step id="mappedStep">
<batchlet ref="MyBatchlet">
<properties>
<property name="xx" value="#{partitionPlan['xx']}" />
</properties>
</batchlet>
<partition>
<mapper ref="MyMapper">
<properties>
<property name="mapperProp" value="#{jobProperties['mapperProp']}" />
</properties>
</mapper>
</partition>
</step>
您的 PartitionMapper 可以使用如下内容构建 PartitionPlan:
import javax.batch.api.partition.PartitionMapper;
import javax.batch.api.partition.PartitionPlan;
import javax.batch.api.partition.PartitionPlanImpl;
// ...
@Named("MyMapper")
public class MyPartitionMapper implements PartitionMapper {
@Inject @BatchProperty
String mapperProp; // FROM JSL, USE IF YOU WANT, NOT USED HERE
@Override
public PartitionPlan mapPartitions() throws Exception {
numPartitions = calculateNumPartitions() // YOUR LOGIC HERE
Properties[] props = new Properties[numPartitions];
Integer i;
for (i = 0; i < numPartitions; i++) {
props[i] = new Properties();
props[i].setProperty("xx", "xxVal" + i); // SUPPLY PER-PARTITION PROPERTY 'xx'
props[i].setProperty("yy", "yyVal" + i); // SUPPLY PER-PARTITION PROPERTY 'yy'
}
PartitionPlan partitionPlan = new PartitionPlanImpl();
partitionPlan.setPartitions(numPartitions);
partitionPlan.setPartitionProperties(props);
partitionPlan.setPartitionsOverride(false);
return partitionPlan;
}
}
我的源 table 有 10 条记录,我需要读取 10 条记录并需要处理这些记录,并且需要将处理过的记录写入目标 table。对于这 10 条记录,我创建了 2 个分区。但是在我的真实项目中,我们不知道我每天需要处理多少条记录,所以我如何决定我需要多少个分区?
是否可以动态添加分区?
请在
使用 PartitionMapper 动态定义分区步骤中的分区数。
这在步骤的开头运行并构建一个 PartitionPlan,它定义分区的数量和每个分区的属性。
在 XML 中,您的 <partition>
元素应包含子 <mapper>
元素而不是您用来静态定义分区数的 <plan>
子元素。
但是您使用 partitionPlan 属性 替换类似地执行替换。
例如
<step id="mappedStep">
<batchlet ref="MyBatchlet">
<properties>
<property name="xx" value="#{partitionPlan['xx']}" />
</properties>
</batchlet>
<partition>
<mapper ref="MyMapper">
<properties>
<property name="mapperProp" value="#{jobProperties['mapperProp']}" />
</properties>
</mapper>
</partition>
</step>
您的 PartitionMapper 可以使用如下内容构建 PartitionPlan:
import javax.batch.api.partition.PartitionMapper;
import javax.batch.api.partition.PartitionPlan;
import javax.batch.api.partition.PartitionPlanImpl;
// ...
@Named("MyMapper")
public class MyPartitionMapper implements PartitionMapper {
@Inject @BatchProperty
String mapperProp; // FROM JSL, USE IF YOU WANT, NOT USED HERE
@Override
public PartitionPlan mapPartitions() throws Exception {
numPartitions = calculateNumPartitions() // YOUR LOGIC HERE
Properties[] props = new Properties[numPartitions];
Integer i;
for (i = 0; i < numPartitions; i++) {
props[i] = new Properties();
props[i].setProperty("xx", "xxVal" + i); // SUPPLY PER-PARTITION PROPERTY 'xx'
props[i].setProperty("yy", "yyVal" + i); // SUPPLY PER-PARTITION PROPERTY 'yy'
}
PartitionPlan partitionPlan = new PartitionPlanImpl();
partitionPlan.setPartitions(numPartitions);
partitionPlan.setPartitionProperties(props);
partitionPlan.setPartitionsOverride(false);
return partitionPlan;
}
}