Kettle User Defined Java Class

Input fields

RowMetaInterface inputRowMeta = getInputRowMeta();

 inputRowMeta对象包含了输入行的元数据,包括域、数据类型、长度、名字、格式等等。例如,查找名字为"customer"的域,可以采用如下方式:

ValueMetaInterface customer = inputRowMeta.searchValueMeta("customer");

 在一个transformation里查找域的名字是很慢的,以为每一条都要查找。建议在第一条记录的时候预先查好:

if (first) {
    yearIndex = getInputRowMeta().indexOfValue(getParameter("YEAR"));
    if (yearIndex<0) {
        throw new KettleException("Year field not found in the input row, check parameter 'YEAR'\!");
}
}

 例如:处理年份这个整形的时候可以这样:

Object[] r = getRow();
...
Long year = inputRowMeta().getInteger(r, yearIndex);

或者,用下面这种简单的方式

Long year = get(Fields.In, "year").getInteger(r);

 

Output fields

可以在“Fields”选项里添加你想要的新域,这样会自动在data.outputRowMeta里增加输出的metadata。

Object[] outputRowData = RowDataUtil.resizeArray(r, data.outputRowMeta.size());

 或者更便于记忆的方式:

Object[] outputRowData = createOutputRow(r, data.outputRowMeta.size());

 

操作输出:

outputRowData[getInputRowMeta().size()] = easterDate(year.intValue());

 或者简易的方式:

get(Fields.Out, "easter").setValue(r, easterDate(year.intValue());
IMPORTANT:

 

以下例子是pentaho官方给出的UDJC例子,但是例子貌似出现了错误,本来是讲firstname拼上lastname,合出一个name字段输出,但是demo里最后一段是

putRow(data.outputRowMeta, r);

而正确的输出应该是

putRow(data.outputRowMeta, outputRow);

 

完整例子如下

String firstnameField;
String lastnameField;
String nameField;
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{
// 获取输入
//
Object[] r = getRow();
// 输入为空,直接false
//
if (r == null) {
setOutputDone();
return false;
}
// 处于性能考虑,parameter只查一次
//
if (first) {
firstnameField = getParameter("FIRSTNAME_FIELD");
lastnameField = getParameter("LASTNAME_FIELD");
nameField = getParameter("NAME_FIELD");
first=false;
}
// 用 createOutputRow() 来保证output的数组够大,能够装下任何新的域
//
Object[] outputRow = createOutputRow(r, data.outputRowMeta.size());
String firstname = get(Fields.In, firstnameField).getString(r);
String lastname = get(Fields.In, lastnameField).getString(r);
// Set the value in the output field
//
String name = firstname+" "+lastname;
get(Fields.Out, nameField).setValue(outputRow, name);
// putRow will send the row on to the default output hop.
//
putRow(data.outputRowMeta, outputRow);
return true;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值