此问题在1个月前或许已经接触,单是一直木有怎么用到,就被耽搁至今;问题的解决要来源于网络,其实我还想说问题的解决更多的是要靠我们自己的思想,不过多的言情,我们接下来直接进入主题吧!
环境:kettle-spoon 4.2.0,oracle11g,myeclipse6.5,sqlserver2008
前提:在kettle图形界面spoon里面已经做好了一个ktr转换模型,此时我的ktr信息如下图:
Step1:在myeclipse创建project,导入kettle集成所需要的包
Step2:重点解析与code源码
//定义ktr名字
private static String transName = "test1";
//初始化kettle环境
KettleEnvironment.init();
//创建资源库对象,此时的对象还是一个空对象
KettleDatabaseRepository repository = new KettleDatabaseRepository();
//创建资源库数据库对象,类似我们在spoon里面创建资源库
DatabaseMeta dataMeta =
new DatabaseMeta("enfo_bi","Oracle","Native","ip","sid","port","username","password");
//资源库元对象,名称参数,id参数,描述等可以随便定义
KettleDatabaseRepositoryMeta kettleDatabaseMeta =
new KettleDatabaseRepositoryMeta("enfo_bi", "enfo_bi", "king description",dataMeta);
//给资源库赋值
repository.init(kettleDatabaseMeta);
//连接资源库
repository.connect("admin","admin");
//根据变量查找到模型所在的目录对象
RepositoryDirectoryInterface directory = repository.findDirectory("/enfo_worker/wxj");
//创建ktr元对象
TransMeta transformationMeta = ((Repository) repository).loadTransformation(transName, directory, null, true, null ) ;
//创建ktr
Trans trans = new Trans(transformationMeta);
//执行ktr
trans.execute(null);
//等待执行完毕
trans.waitUntilFinished();
上面的两个步骤才可以确定是资源库中的那个路径下的ktr和我们用命令执行一样的-dir ,-tran -job
附上源码:
package kettle; import org.pentaho.di.core.KettleEnvironment; import org.pentaho.di.core.database.DatabaseMeta; import org.pentaho.di.core.exception.KettleException; import org.pentaho.di.repository.Repository; import org.pentaho.di.repository.RepositoryDirectoryInterface; import org.pentaho.di.repository.kdr.KettleDatabaseRepository; import org.pentaho.di.repository.kdr.KettleDatabaseRepositoryMeta; import org.pentaho.di.trans.Trans; import org.pentaho.di.trans.TransMeta; /** * <p>Title: java调用kettle4.2数据库型资料库中的转换</p> * <p>Description: </p> * <p>Copyright: Copyright () 2012</p> */ public class ExecuteDataBaseRepTran {
private static String transName = "test1"; public static void main(String[] args) { try { //初始化kettle环境 KettleEnvironment.init(); //创建资源库对象,此时的对象还是一个空对象 KettleDatabaseRepository repository = new KettleDatabaseRepository(); //创建资源库数据库对象,类似我们在spoon里面创建资源库 DatabaseMeta dataMeta = new DatabaseMeta("enfo_bi","Oracle","Native","ip","sid","port","username","password"); //资源库元对象,名称参数,id参数,描述等可以随便定义 KettleDatabaseRepositoryMeta kettleDatabaseMeta = new KettleDatabaseRepositoryMeta("enfo_bi", "enfo_bi", "king description",dataMeta); //给资源库赋值 repository.init(kettleDatabaseMeta); //连接资源库 repository.connect("admin","admin"); //根据变量查找到模型所在的目录对象,此步骤很重要。 RepositoryDirectoryInterface directory = repository.findDirectory("/enfo_worker/wxj"); //创建ktr元对象 TransMeta transformationMeta = ((Repository) repository).loadTransformation(transName, directory, null, true, null ) ; //创建ktr Trans trans = new Trans(transformationMeta); //执行ktr trans.execute(null); //等待执行完毕 trans.waitUntilFinished(); if(trans.getErrors()>0) { System.err.println("Transformation run Failure!"); } else { System.out.println("Transformation run successfully!"); } } catch (KettleException e) { e.printStackTrace(); } } }