Kettle与Java集成——Java代码调取运行资源库的Transformation

下面代码是Java代码调用Kettle4.0版本的Transformation

1Java读取资源库运行Transformation

(1)目录类型资源库Java代码

  1. import org.pentaho.di.core.KettleEnvironment;
  2. import org.pentaho.di.core.exception.KettleException;
  3. import org.pentaho.di.repository.filerep.KettleFileRepository;
  4. import org.pentaho.di.repository.filerep.KettleFileRepositoryMeta;
  5. import org.pentaho.di.trans.Trans;
  6. import org.pentaho.di.trans.TransMeta;
  7. public class ReaderTransFromRep {
  8. private static String tranName = "IM_KSDM"; // 传输名称
  9. public static void main(String[] args) throws KettleException {
  10. Trans trans= null;
  11. try {
  12. // 初始化
  13. KettleEnvironment.init();这个是 4.0版本的初始化,和 3.x版本的不同
  14. // 资源库元对象
  15. KettleFileRepositoryMeta repinfo = new KettleFileRepositoryMeta( "", "", "数据采集", "file:///E:/Workspaces/Kettle");
  16. // 文件形式的资源库
  17. KettleFileRepository rep = new KettleFileRepository();
  18. rep.init(repinfo);
  19. // 转换元对象
  20. if(tranName!= null){
  21. TransMeta transMetadel = rep.loadTransformation(rep.getTransformationID(tranName, null), null);
  22. // 转换
  23. trans = new Trans(transMetadel);
  24. // 执行转换
  25. trans.execute( null); 
  26. // 等待转换执行结束
  27. trans.waitUntilFinished();
  28. //抛出异常
  29. if(trans.getErrors()> 0){
  30. throw new Exception( "There are errors during transformation exception!(传输过程中发生异常)");
  31. }
  32. } else{
  33. throw new KettleException( "传输名为空!");
  34. }
  35. } catch (Exception e) {
  36. if(trans!= null){
  37. trans.stopAll();
  38. }
  39. e.printStackTrace();
  40. throw new KettleException(e);
  41. }
  42. }
  43. }


 
 
 

(2)数据库类型资源库

  1. import org.pentaho.di.core.KettleEnvironment;
  2. import org.pentaho.di.core.database.DatabaseMeta;
  3. import org.pentaho.di.core.exception.KettleException;
  4. import org.pentaho.di.repository.RepositoryDirectory;
  5. import org.pentaho.di.repository.RepositoryDirectoryInterface;
  6. import org.pentaho.di.repository.kdr.KettleDatabaseRepository;
  7. import org.pentaho.di.repository.kdr.KettleDatabaseRepositoryMeta;
  8. import org.pentaho.di.trans.Trans;
  9. import org.pentaho.di.trans.TransMeta;
  10. public class ReaderTransFromDBRep {
  11. private static String transName= "t1";
  12. public static void main(String[] args) {
  13. try {
  14. KettleEnvironment.init();
  15. DatabaseMeta dataMeta = new DatabaseMeta( "KettleDBRep", "MSSQL", "Native", "127.0.0.1", "etl", "1433", "sa", "bsoft");
  16. KettleDatabaseRepositoryMeta repInfo = new KettleDatabaseRepositoryMeta();
  17. repInfo.setConnection(dataMeta);
  18. KettleDatabaseRepository rep = new KettleDatabaseRepository();
  19. rep.init(repInfo);
  20. rep.connect( "admin", "admin");
  21. RepositoryDirectoryInterface dir = new RepositoryDirectory();
  22. dir.setObjectId(rep.getRootDirectoryID());
  23. TransMeta tranMeta = rep.loadTransformation(rep.getTransformationID(transName, dir), null);
  24. Trans trans = new Trans(tranMeta);
  25. trans.execute( null);
  26. trans.waitUntilFinished();
  27. } catch (KettleException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值