java代碼組件的使用
連接資源庫(kù)(可參考入門篇(一))
新建轉(zhuǎn)換
需求:根據(jù)身份證确徙,計(jì)算得出年齡和出生年月。這里我們用生成記錄組件,來模擬從表中獲取到數(shù)據(jù)。
生成記錄組件:
配置java代碼
// 生成的代碼片段
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
if (first) {
first = false;
/* TODO: Your code here. (Using info fields)
FieldHelper infoField = get(Fields.Info, "info_field_name");
RowSet infoStream = findInfoRowSet("info_stream_tag");
Object[] infoRow = null;
int infoRowCount = 0;
// Read all rows from info step before calling getRow() method, which returns first row from any
// input rowset. As rowMeta for info and input steps varies getRow() can lead to errors.
while((infoRow = getRowFrom(infoStream)) != null){
// do something with info data
infoRowCount++;
}
*/
}
Object[] r = getRow();
if (r == null) {
setOutputDone();
return false;
}
// It is always safest to call createOutputRow() to ensure that your output row's Object[] is large
// enough to handle any new fields you are creating in this step.
r = createOutputRow(r, data.outputRowMeta.size());
/* TODO: Your code here. (See Sample)
// Get the value from an input field
String foobar = get(Fields.In, "a_fieldname").getString(r);
foobar += "bar";
// Set a value in a new output field
get(Fields.Out, "output_fieldname").setValue(r, foobar);
*/
// Send the row on to the next step.
putRow(data.outputRowMeta, r);
return true;
}
點(diǎn)擊main可以生成java代碼的模板域醇。如果需要修改,我們只要在putRow(data.outputRowMeta, r);
寫自己的邏輯代碼即可蓉媳。
根據(jù)我們的需求譬挚,我們需要根據(jù)身份證計(jì)算年齡和生日。所以接下來酪呻,我們只要在它標(biāo)記的TODO: Your code here. (See Sample)
開始編寫自己的邏輯代碼即可减宣。
首先需要獲取從生成記錄中獲取到我們的身份證信息。
編寫邏輯代碼
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
if (first) {
first = false;
}
Object[] r = getRow();
if (r == null) {
setOutputDone();
return false;
}
r = createOutputRow(r, data.outputRowMeta.size());
//身份證
String idCard = get(Fields.In, "idCard").getString(r);
// 判斷身份證是否為空(這里暫時(shí)只做為空校驗(yàn)玩荠,暫不做正則)
// 321111199501010101
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
try {
if(null != idCard && idCard.length() == 18){
String substring = idCard.substring(6, 14);
Date parse = format.parse(substring);
if(parse != null){
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
start.setTime(parse);
end.setTimeInMillis(System.currentTimeMillis());
long age = end.get(Calendar.YEAR) - start.get(Calendar.YEAR);
get(Fields.Out, "age").setValue(r, age);
get(Fields.Out, "birth").setValue(r, parse);
logBasic("age: " + age);
logBasic("birth: " + parse);
}
}else{
throw new Exception("身份證號(hào)不規(guī)范蚪腋!無法解析");
}
} catch (Exception e) {
e.printStackTrace();
}
putRow(data.outputRowMeta, r);
return true;
}
運(yùn)行轉(zhuǎn)換
注意點(diǎn):
獲取變量
get(Fields.In, "idCard").getString(r);
根據(jù)變量類型的不同丰歌,getString需要變換姨蟋。設(shè)置變量
get(Fields.Out, "birth").setValue(r, parse);
-
日志輸出
logBasic("age: " + age);
導(dǎo)包屉凯;
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date;
需要java環(huán)境或者Lib下的jar包里的類,包括kettle自帶的類都行輸出變量眼溶,如果輸入與輸出的變量一樣悠砚,可以不寫。如果不一樣堂飞,需要在下面填寫新的變量灌旧,如圖所示:
- 最后不要忘記
putRow(data.outputRowMeta, r);及return true;