在使用kettle过程中,有些功能是kettle不提供的,这样就需要想办法,不过kettle中的java代码可以解决大部分问题,下边就展示使用java代码组件调用自己编写的jar包的过程
1,创建java-jar包
package test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class TestFun {
public String to_upper(String str)
{
String str1=str.toUpperCase();
BufferedWriter writer = null;
File file = new File("d:\\dat\\"+ "aaa" + ".json");
//如果文件不存在,则新建一个
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//写入
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,false), "UTF-8"));
writer.write("aaa");
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(writer != null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return str1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestFun tf = new TestFun();
System.out.println(tf.to_upper("china"));
}
}
调试,没问题后,导出jar
2,将jar包放入kettle中的lib目录,并重启kettle
3,创建ktr并测试
整体流程
其中从数据库中输入表行,经过java代码处理输出为文本文件
其中java代码内容如下:
//package com.tools.ws;
import test.TestFun;
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{
Object[] r = getRow();
// If the row object is null, we are done processing.
if (r == null) {
setOutputDone();
return false;
}
//if(first){
// first = false;
//}
TestFun tf = new TestFun();
r = createOutputRow(r, data.outputRowMeta.size());
String inputstr = get(Fields.In,"str").getString(r);
String upperstr = tf.to_upper(inputstr);
String upperstr2 = inputstr.toUpperCase();
get(Fields.Out, "outputstr1").setValue(r, upperstr);
get(Fields.Out, "outputstr2").setValue(r, upperstr2);
putRow(data.outputRowMeta, r);
return true;
}
以下是截图
测试截图: