转至:
http://dsbjoe.iteye.com/blog/1151058
Input fields
Java代码
1.RowMetaInterface inputRowMeta = getInputRowMeta();
inputRowMeta对象包含了输入行的元数据,包括域、数据类型、长度、名字、格式等等。例如,查找名字为"customer"的域,可以采用如下方式:
Java代码
1.ValueMetaInterface customer = inputRowMeta.searchValueMeta("customer");
在一个transformation里查找域的名字是很慢的,以为每一条都要查找。建议在第一条记录的时候预先查好:
Java代码
1.if (first) {
2. yearIndex = getInputRowMeta().indexOfValue(getParameter("YEAR"));
3. if (yearIndex<0) {
4. throw new KettleException("Year field not found in the input row, check parameter 'YEAR'\!");
5.}
6.}
例如:处理年份这个整形的时候可以这样:
Java代码
1.Object[] r = getRow();
2....
3.Long year = inputRowMeta().getInteger(r, yearIndex);
或者,用下面这种简单的方式
Java代码
1.Long year = get(Fields.In, "year").getInteger(r);
Output fields
可以在“Fields”选项里添加你想要的新域,这样会自动在data.outputRowMeta里增加输出的metadata。
Java代码
1.Object[] outputRowData = RowDataUtil.resizeArray(r, data.outputRowMeta.size());
或者更便于记忆的方式:
Java代码
1.Object[] outputRowData = createOutputRow(r, data.outputRowMeta.size());
操作输出:
Java代码
1.outputRowData[getInputRowMeta().size()] = easterDate(year.intValue());
或者简易的方式:
Java代码
1.get(Fields.Out, "easter").setValue(r, easterDate(year.intValue());
2.IMPORTANT:
以下例子是pentaho官方给出的UDJC例子,但是例子貌似出现了错误,本来是讲firstname拼上lastname,合出一个name字段输出,但是demo里最后一段是
Java代码
1.putRow(data.outputRowMeta, r);
而正确的输出应该是
Java代码
1.putRow(data.outputRowMeta, outputRow);
完整例子如下 :
Java代码
1.String firstnameField;
2.String lastnameField;
3.String nameField;
4.public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
5.{
6.// 获取输入
7.//
8.Object[] r = getRow();
9.// 输入为空,直接false
10.//
11.if (r == null) {
12.setOutputDone();
13.return false;
14.}
15.// 处于性能考虑,parameter只查一次
16.//
17.if (first) {
18.firstnameField = getParameter("FIRSTNAME_FIELD");
19.lastnameField = getParameter("LASTNAME_FIELD");
20.nameField = getParameter("NAME_FIELD");
21.first=false;
22.}
23.// 用 createOutputRow() 来保证output的数组够大,能够装下任何新的域
24.//
25.Object[] outputRow = createOutputRow(r, data.outputRowMeta.size());
26.String firstname = get(Fields.In, firstnameField).getString(r);
27.String lastname = get(Fields.In, lastnameField).getString(r);
28.// Set the value in the output field
29.//
30.String name = firstname+" "+lastname;
31.get(Fields.Out, nameField).setValue(outputRow, name);
32.// putRow will send the row on to the default output hop.
33.//
34.putRow(data.outputRowMeta, outputRow);
35.return true;
36.}
http://dsbjoe.iteye.com/blog/1151058
Input fields
Java代码
1.RowMetaInterface inputRowMeta = getInputRowMeta();
inputRowMeta对象包含了输入行的元数据,包括域、数据类型、长度、名字、格式等等。例如,查找名字为"customer"的域,可以采用如下方式:
Java代码
1.ValueMetaInterface customer = inputRowMeta.searchValueMeta("customer");
在一个transformation里查找域的名字是很慢的,以为每一条都要查找。建议在第一条记录的时候预先查好:
Java代码
1.if (first) {
2. yearIndex = getInputRowMeta().indexOfValue(getParameter("YEAR"));
3. if (yearIndex<0) {
4. throw new KettleException("Year field not found in the input row, check parameter 'YEAR'\!");
5.}
6.}
例如:处理年份这个整形的时候可以这样:
Java代码
1.Object[] r = getRow();
2....
3.Long year = inputRowMeta().getInteger(r, yearIndex);
或者,用下面这种简单的方式
Java代码
1.Long year = get(Fields.In, "year").getInteger(r);
Output fields
可以在“Fields”选项里添加你想要的新域,这样会自动在data.outputRowMeta里增加输出的metadata。
Java代码
1.Object[] outputRowData = RowDataUtil.resizeArray(r, data.outputRowMeta.size());
或者更便于记忆的方式:
Java代码
1.Object[] outputRowData = createOutputRow(r, data.outputRowMeta.size());
操作输出:
Java代码
1.outputRowData[getInputRowMeta().size()] = easterDate(year.intValue());
或者简易的方式:
Java代码
1.get(Fields.Out, "easter").setValue(r, easterDate(year.intValue());
2.IMPORTANT:
以下例子是pentaho官方给出的UDJC例子,但是例子貌似出现了错误,本来是讲firstname拼上lastname,合出一个name字段输出,但是demo里最后一段是
Java代码
1.putRow(data.outputRowMeta, r);
而正确的输出应该是
Java代码
1.putRow(data.outputRowMeta, outputRow);
完整例子如下 :
Java代码
1.String firstnameField;
2.String lastnameField;
3.String nameField;
4.public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
5.{
6.// 获取输入
7.//
8.Object[] r = getRow();
9.// 输入为空,直接false
10.//
11.if (r == null) {
12.setOutputDone();
13.return false;
14.}
15.// 处于性能考虑,parameter只查一次
16.//
17.if (first) {
18.firstnameField = getParameter("FIRSTNAME_FIELD");
19.lastnameField = getParameter("LASTNAME_FIELD");
20.nameField = getParameter("NAME_FIELD");
21.first=false;
22.}
23.// 用 createOutputRow() 来保证output的数组够大,能够装下任何新的域
24.//
25.Object[] outputRow = createOutputRow(r, data.outputRowMeta.size());
26.String firstname = get(Fields.In, firstnameField).getString(r);
27.String lastname = get(Fields.In, lastnameField).getString(r);
28.// Set the value in the output field
29.//
30.String name = firstname+" "+lastname;
31.get(Fields.Out, nameField).setValue(outputRow, name);
32.// putRow will send the row on to the default output hop.
33.//
34.putRow(data.outputRowMeta, outputRow);
35.return true;
36.}