java中调用kettle转换文件

java中调用kettle转换文件

通过命令行也可以调用,然后java中调用命令行代码也可以;这样没有和java代码逻辑无缝集成。本文说明kettle5.1中如果通过其他API和java代码无缝集成;网上大多数资料都是低版本的,在kettle5.x中已经不能运行。

1、 需要哪些jar文件

\

以kettle开头的是必须,上图最下面三个也要;红色框中的两个是我测试转换用到的,分别是生成UUID和文件。

要是少了jar文件,运行程序一定报错,大家根据错误到kettle安装目录LIB中找相应的jar加到编译路径中。

2、 示例说明如何通过java调用转换

示例是把一个excel的内容导入到数据中,excel只有两列,所以需要在kettle中生成一列uuid,然后导入到数据库中。

\

默认生成的uuid有‘-’间隔符,所以通过“Replace in string”替换为空;

\

excel步骤,使用了命名参数,所以要在转换配置设置命名参数。

\


实例代码如下:

package kettle;


import java.io.File;
import java.util.Map;


import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;


public class KettleUtil2 {
 
  public String RES_DIR = "res";
  private String fullFileName ;


  public KettleUtil2(String fileName){
     fullFileName = System.getProperty("user.dir") + File.separator + RES_DIR;
     fullFileName += File.separator + fileName;
  }
 
  /**
   * 没有参数是,设置参数为null
   * @param paras
   */
  public void runTransformation(Map<String,String> paras) {
     try {
       KettleEnvironment.init();
       TransMeta transMeta = new TransMeta(fullFileName);
       Trans  transformation =new Trans(transMeta);
       for(Map.Entry<String, String> entry: paras.entrySet()) {
           transformation.setParameterValue(entry.getKey(), entry.getValue());
       }
       transformation.execute(null);
       transformation.waitUntilFinished();
       if (transformation.getErrors() > 0) {
          throw new RuntimeException("There wereerrors during transformation execution.");
       }
     } catch (KettleException e) {
       System.out.println(e);
     }
  }
 
}


public class EtlTest {
 
  public static void main(String[] args) {
     KettleUtil2 etl = new KettleUtil2("device_subtype.ktr");
     Map<String,String> para = new HashMap<String,String>();
     //给转换中命名参数赋值
     para.put("XlsName", "data");
     etl.runTransformation(para);
  }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值