java代码组件的使用
连接资源库(可参考入门篇(一))
新建转换
需求:根据身份证,计算得出年龄和出生年月。这里我们用生成记录组件,来模拟从表中获取到数据。
生成记录组件:
配置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;
}
点击main可以生成java代码的模板。如果需要修改,我们只要在putRow(data.outputRowMeta, r);
写自己的逻辑代码即可。
根据我们的需求,我们需要根据身份证计算年龄和生日。所以接下来,我们只要在它标记的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);
// 判断身份证是否为空(这里暂时只做为空校验,暂不做正则)
// 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("身份证号不规范!无法解析");
}
} catch (Exception e) {
e.printStackTrace();
}
putRow(data.outputRowMeta, r);
return true;
}
运行转换
注意点:
获取变量
get(Fields.In, "idCard").getString(r);
根据变量类型的不同,getString需要变换。设置变量
get(Fields.Out, "birth").setValue(r, parse);
-
日志输出
logBasic("age: " + age);
导包;
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date;
需要java环境或者Lib下的jar包里的类,包括kettle自带的类都行输出变量,如果输入与输出的变量一样,可以不写。如果不一样,需要在下面填写新的变量,如图所示:
- 最后不要忘记
putRow(data.outputRowMeta, r);及return true;