MY source table is having 10 records and I need to read 10 records and needs to process those and needs to write that processed records to the target table. For this 10 records I have created 2 partitions. But in my real project we don't know how many records daily i need to process so how can I decide that how many partitions do i need? Is it possible to add partitions dynamically? Please find my poc code at JSR 352 with Liberty Profile - how to implement checkpointing when ItemReader does a DB query.
Asked
Active
Viewed 423 times
1 Answers
2
Use a PartitionMapper to dynamically define the number of partitions in a partitioned step.
This runs at the beginning of the step and builds a PartitionPlan which defines the number of partitions and the Properties for each partition.
In the XML, your <partition>
element should include a child <mapper>
element rather than the child <plan>
element you'd use to define the number of partitions statically.
But you otherwise perform the substitution similarly using the partitionPlan property substitution.
E.g.
<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>
Your PartitionMapper could build the PartitionPlan with something like this:
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;
}
}

Scott Kurz
- 4,985
- 1
- 18
- 40
-
Thx Scott for your help.Actually I am not able to find any sample code to implement partition mapper. Can you please help me where can I find the sample.One more issue like i need to pass different parameters to each partition to fetch data as my source table is having timestamp only so that i need to pass 12AM to 6 PM timestamp rows into first partition and 6PM to 12AM partitions to another partition. – Srinivas K Jun 10 '16 at 09:18
-
thank you for your quick help. I will try this example. – Srinivas K Jun 10 '16 at 13:38