Multithreading 如何为MultiResourceItemReader应用分区计数?
我有一个5万条记录的档案。将其插入数据库需要将近40分钟。因此,我考虑对该步骤应用一个分区,这样50k条记录就可以在10个线程之间进行分区(通过Multithreading 如何为MultiResourceItemReader应用分区计数?,multithreading,spring-batch,Multithreading,Spring Batch,我有一个5万条记录的档案。将其插入数据库需要将近40分钟。因此,我考虑对该步骤应用一个分区,这样50k条记录就可以在10个线程之间进行分区(通过gridSize),每个线程并行处理1000条记录 所有论坛都展示了通过执行上下文使用JDBCPagingItemReader和分区计数集的示例。 由于我正在使用MultiResourceItemReader,如何设置MultiResourceItemReader的分区计数(startingIndex和endingIndex-请参阅下面的代码片段) 请告
gridSize
),每个线程并行处理1000条记录
所有论坛都展示了通过执行上下文使用JDBCPagingItemReader
和分区计数集的示例。
由于我正在使用MultiResourceItemReader
,如何设置MultiResourceItemReader
的分区计数(startingIndex
和endingIndex
-请参阅下面的代码片段)
请告知
下面是分区器的代码段:
public Map partition(int gridSize) {
LOGGER.debug("START: Partition");
Map partitionMap = new HashMap();
int startingIndex = 0;
int endingIndex = 1000;
for(int i=0; i< gridSize; i++){
ExecutionContext ctxMap = new ExecutionContext();
ctxMap.putInt("startingIndex",startingIndex);
ctxMap.putInt("endingIndex", endingIndex);
startingIndex = endingIndex+1;
endingIndex += 1000;
partitionMap.put("Thread:-"+i, ctxMap);
}
LOGGER.debug("END: Created Partitions of size: "+ partitionMap.size());
return partitionMap;
}
公共地图分区(int gridSize){
debug(“启动:分区”);
Map partitionMap=newhashmap();
int startingIndex=0;
int-endingIndex=1000;
对于(int i=0;i
您没有在MultiResourceItemReader
上设置分区计数。您可以使用MultiResourcePartitioner
为每个资源(文件)创建一个分区,然后让读取器将每个文件作为自己的分区分别提取。使用该配置,您不再需要MultiResourceItemReader
(您可以直接转到代理)
Spring批处理示例中有此用例的一个示例,可在此处找到:感谢您的回复。我不想要每个资源(文件)的分区。我希望对单个文件进行分区。这对我的场景有效吗?在单个文件中进行分区通常不会提高性能,因为进程通常是IO绑定的。这里的瓶颈是什么?50k记录应该不会花那么长时间插入,除非有其他瓶颈…我正在努力找到瓶颈。是否有任何方法可以创建多个线程并配置每个线程以根据提交间隔处理特定的记录计数?是否可以拆分文件?