设为首页 加入收藏

TOP

详解kettle之UserDefinedJavaClass步骤(二)(一)
2015-07-24 10:26:35 来源: 作者: 【 】 浏览:5
Tags:详解 kettle UserDefinedJavaClass 步骤
详解User Defined Java Class步骤(二)

kettle中的“user defined java class”步骤,也称UDJC步骤,从4.0版本就有,功能非常强大,无所不能;可以在其中写任意代码,却不影响效率。本文将详细介绍在不同场景中用示例展示如果使用该步骤,由于内容非常多,便于阅读方便,把内容分成三部分,请完整看完全部内容,示例代码在这里下载.

如果没有从第一部分开始,请访问第一部分。

使用步骤参数(Step Parameter)

如果你写了一段代码,如果想让带更通用,步骤参数这时就能用到;在示例中,我们提供一个正则表达式和一个字段的名称,该步骤检查参数对应的字段是否匹配正则表达式,如果是返回结果为1,反之为0。

\

代码如下:

import java.util.regex.Pattern;

?

private Pattern p = null;

private FieldHelper fieldToTest = null;

private FieldHelper outputField = null;

?

public boolean processRow(StepMetaInterfacesmi, StepDataInterface sdi) throws KettleException

{

Object[] r = getRow();

if (r == null) {

setOutputDone();

return false;

}

// prepare regex and field helpers

if (first){

first = false;

String regexString = getParameter("regex");

p = Pattern.compile(regexString);

fieldToTest = get(Fields.In, getParameter("test_field"));

outputField = get(Fields.Out, "result");

}

r= createOutputRow(r, data.outputRowMeta.size());

?

// Get the value from an input field

String test_value = fieldToTest.getString(r);

// test for match and write result

if (p.matcher(test_value).matches()){

outputField.setValue(r, Long.valueOf(1));

}

else{

outputField.setValue(r, Long.valueOf(0));

}

// Send the row on to the next step.

putRow(data.outputRowMeta, r);

return true;

}

\

getParameter()方法返回在ui界面中定义的参数对应值内容,当然参数的值也可能是kettle的变量。把变量作为参数是使用变量通常的做法。我们可以在步骤的xml代码中手工搜索到变量。

示例的转换名称是:parameter.ktr.

?

消息步骤(Info Steps)使用

有时需要合并多个输入步骤,可能赋予不同的角色,就如流查询步骤。消息步骤用来提供查询,其数据行不通过getRow()方法返回。在udjc步骤中非常容易使用。在udjc步骤的ui界面消息步骤选项卡中定义,通过getRowsFrom()方法返回对应的值。

示例转换中使用消息步骤接收一组正则表达式,用其测试主流数据中的一个字段是否匹配,如果任何一个表达式匹配,结果字段设置为1.如果没有任何匹配,则结果为0,同时附加输出匹配的表达式。

\

代码如下:

import java.util.regex.Pattern;

import java.util.*;

?

private FieldHelper resultField = null;

private FieldHelper matchField = null;

private FieldHelper outputField = null;

private FieldHelper inputField = null;

private ArrayList patterns = newArrayList(20);

private ArrayList expressions = newArrayList(20);

?

public boolean processRow(StepMetaInterfacesmi, StepDataInterface sdi) throws KettleException

{

Object[] r = getRow();

?

if (r == null) {

setOutputDone();

return false;

}

?

// prepare regex and field helpers

if (first){

first = false;

// get the input and output fields

resultField = get(Fields.Out, "result");

matchField = get(Fields.Out, "matched_by");

inputField = get(Fields.In, "value");

?

// get all rows from the info stream andcompile the regex field to patterns

FieldHelper regexField = get(Fields.Info, "regex");

RowSet infoStream = findInfoRowSet("expressions");

?

Object[] infoRow = null;

while((infoRow = getRowFrom(infoStream)) != null){

String regexString = regexField.getString(infoRow);

expressions.add(regexString);

patterns.add(Pattern.compile(regexString));

}

?

}

?

// get the value of the field to check

String value = inputField.getString(r);

?

// check if any pattern matches

int matchFound = 0;

String matchExpression = null;

for(int i=0;i

if (((Pattern) patterns.get(i)).matcher(value).matches()){

matchFound = 1;

matchExpression = (String)expressions.get(i);

break;

}

}

?

// write result to stream

r= createOutputRow(r, data.outputRowMeta.size());

resultField.setValue(r, Long.valueOf(matchFound));

matchField.setValue(r, matchExpression);

?

//

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Chapter1SecuringYourServerandNe.. 下一篇【数据挖掘导论】――数据类型

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Python爬虫教程(从 (2025-12-26 16:49:14)
·【全269集】B站最详 (2025-12-26 16:49:11)
·Python爬虫详解:原 (2025-12-26 16:49:09)
·Spring Boot Java: (2025-12-26 16:20:19)
·Spring BootでHello (2025-12-26 16:20:15)