2014-10-22 52 views
2

我是Talend Open Studio的初學者,我正在嘗試進行下面的轉換。我們是否可以使用Talend Open Studio聚合動態行數

從包含SQL表:

DeltaStock   Date 
------------------------ 
+50 (initial stock) J0  
+80     J1  
-30     J2 
...     ... 

我想生產這種表:

Stock Date 
----------- 
50  J0    
130 J1 
100 J2 
... ... 

你覺得這可能使用TOS是可能的嗎?我想到使用tAggregateRow,但我沒有發現它適合我的問題。

回答

0

使用tMemorizeRows組件可能會有更簡單的方法來完成此任務,但想到的第一個想法是使用globalMap來存儲滾動總和。

在Talend中,可以在globalMap中存儲一個對象(任何值或任何類型),以便稍後在作業中檢索它。如果您曾經使用tFlowToIterate組件,它允許您從全局映射中檢索正在迭代的行的值,則會自動使用它。

一個非常基本的樣本作業可能是這樣的:

Sample job layout

在此,我們有一個tJava組件,只有初始化的辦事處一覽滾動和用下面的代碼:

//Initialise the rollingSum global variable 
globalMap.put("rollingSum", 0); 

在此之後,我們將這個組件連接到SubjobOk上,以確保我們只有在設法將rollingSum放入globalMap時才能繼續。

然後我使用tFixedFlowInput組件提供我的數據,該組件允許我輕鬆地爲此示例作業硬編碼一些值。您可以輕鬆地將其替換爲任何輸入。我從這個問題用你的樣本輸入數據:

tFixedFlowInput configuration and sample data

然後,我們使用tJavaRow將由行做的數據行一些轉換處理數據。我用下面的代碼適用於這個例子:

//Initialise the operator and the value variables 
String operator = ""; 
Integer value = 0; 

//Get the current rolling sum 
Integer rollingSum = (Integer) globalMap.get("rollingSum"); 

//Extract the operator 
Pattern p = Pattern.compile("^([+-])([0-9]+)$"); 
Matcher m = p.matcher(input_row.deltaStock); 

//If we have any matches from the regular expression search then extract the operator and the value 
if (m.find()) { 
    operator = m.group(1); 
    value = Integer.parseInt(m.group(2)); 
} 

//Conditional to use the operator 
if ("+".equals(operator)) { 
    rollingSum += value; 
} else if ("-".equals(operator)) { 
    rollingSum -= value; 
} else { 
    System.out.println("The operator provided wasn't a + or a -"); 
} 

//Put the new rollingSum back into the globalMap 
globalMap.put("rollingSum", rollingSum); 

//Output the data 
output_row.stock = rollingSum; 
output_row.date = input_row.date; 

有不少那兒的情況,但基本上它開始從辦事處一覽獲取當前rollingSum

接下來,它使用正則表達式將deltaStock字符串拆分爲operatorvalue。由此它使用提供的運營商(加號或減號)將deltaStock添加到rollingSum或從rollingSum中減去deltaStock

之後,它將新的rollingSum添加回globalMap,並輸出stockdate(不變)的2列。

在我的示例工作中,我使用tLogRow輸出數據,它將數據的值打印到控制檯。我通常選擇表格格式選項,在這種情況下,我得到以下輸出:

.-----+----. 
|tLogRow_8 | 
|=----+---=| 
|stock|date| 
|=----+---=| 
|50 |J0 | 
|130 |J1 | 
|100 |J2 | 
'-----+----' 

這應該是你在找什麼。

0

你應該可以在Talend Open Studio中做到這一點。

我在這裏附上一張圖片,裏面有JOB,tJavaRow的內容和執行結果。 enter image description here

我在tFixedFlowInput下面用來模擬輸入一個tJDBCInput,你應該用它來讀取數據庫中的數據。希望你可以使用特定的tXXXInput作爲你的數據庫,而不是通用的JDBC。

以下是tJavaRow中的一些簡單代碼。

//Code generated according to input schema and output schema 
output_row.delta = input_row.delta; 
output_row.date = input_row.date; 
output_row.rollingSum = 
    Integer.parseInt(globalMap.get("rollingSum").toString()); 

int delta = Integer.parseInt(input_row.delta); 

output_row.rollingSum += delta; 

// Save rolling SUM for next round 
globalMap.put("rollingSum", output_row.rollingSum); 

請注意parseInt()中的例外情況。你應該以你認爲正確的方式處理它們。 在我的項目中,我通常有一個SafeParse庫,它不會拋出異常,但會返回一個默認值,我可以將它與vale一起傳遞以進行解析。

相關問題