kettle调用自定义Java代码

        有时候需要对Kettle抽取过来的数据进行加解密或者其他一些自定义的操作,Kettle本身有对称加密和解密的功能,但由于有时候需要自定义,所以还是提供了调用外部接口的能力。

       新建一个自定义处理的JAVA类,例如:

package test; 
public class Test{ 
    public static final String getMyName(String name){ 
     //your code   
     return name; 
    } 
} 

   处理好你的逻辑以后,打包成一个JAR包,放到KEttl的 lib目录里面,然后 在Java代码控件填写调用逻辑

import test.Test; 
 
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException 
{ 
    Object[] r = getRow(); 
    if (r == null) { 
        setOutputDone(); 
        return false; 
    } 
 
    if (first) 
    { 
        first = 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, outputRowSize); 
     
    /* 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); 
 
    */ 
 
    //调用jar 
    String name= get(Fields.In, "ENAME").getString(r); //输入参数 
    name= Test.getMyName(name); 
    get(Fields.Out, "ENAME").setValue(r, name); 
 
     
    //获取参数 
     String AGEField = getParameter("AGE"); 
     get(Fields.Out, "AGE").setValue(r, AGEField); //输出参数 
     
 
    // Send the row on to the next step. 
    putRow(data.outputRowMeta, r); 
 
    return true; 
} 

  运行转换测试数据是否正确

  

  

   

猜你喜欢

转载自chenhua-1984.iteye.com/blog/2119393