2015-06-18 31 views
0

下面是我的作業配置分區的一個步驟。如何根據數據庫卷分區步驟

<!-- master step, 10 threads (grid-size) --> 
    <step id="masterStep"> 
     <partition step="slave" partitioner="rangePartitioner"> 
      <handler grid-size="10" task-executor="taskExecutor" /> 
     </partition> 
    </step> 

</job> 

<!-- Jobs to run --> 
<step id="slave" xmlns="http://www.springframework.org/schema/batch"> 
    <tasklet> 
     <chunk reader="pagingItemReader" writer="jdbcWriter" 
      processor="itemProcessor" commit-interval="1" /> 
    </tasklet> 
</step> 

以下是從上面的配置,我可以能夠與分配工作,我的分區java類

Map<String, ExecutionContext> result = new HashMap<String, ExecutionContext>(); 

    int range = 5; 
    int fromId = 1; 
    int toId = range; 

    for (int i = 1; i <= gridSize; i++) { 
     ExecutionContext value = new ExecutionContext(); 

     System.out.println("\nStarting : Thread" + i); 
     System.out.println("fromId : " + fromId); 
     System.out.println("toId : " + toId); 

     value.putInt("fromId", fromId); 
     value.putInt("toId", toId); 

     // give each thread a name 
     value.putString("name", "Thread" + i); 

     result.put("partition" + i, value); 

     fromId = toId + 1; 
     toId += range; 

    } 

    return result; 

,10個線程將被運行,每個線程將處理5個記錄。

有2種情況

1.如果僅存在5記錄剩餘的9個線程正在處理沒有數據 2.如果存在的數據量經常變化有一天爲5次的記錄和一個那麼在這種情況下就會有100萬條記錄,如果我在文件中硬編碼它們,我就不能每天配置網格的大小和範圍。

所以現在我的要求是動態線程應該基於數據庫的容量進行處理,即基於特定表中的行數。

請爲此場景建議我。

回答

0

在Spring Batch中有一個用於分區數據庫記錄的示例。該示例使用名爲ColumnRangePartitioner的類。該類演示如何查詢最小和最大id,然後基於該分區進行分區。你可以直接從Github中獲取這個類,並在你自己的代碼中使用它,或者直接應用這個principal。