Home > Database > Mysql Tutorial > body text

java中调用kettle转换文件

WBOY
Release: 2016-06-07 16:09:51
Original
1926 people have browsed it

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

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步骤,使用了命名参数,所以要在转换配置设置命名参数。

\

示例代码如下:

publicclass 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

    */

   publicvoid 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) {

           thrownew RuntimeException(

                 "There wereerrors during transformation execution.");

        }

      } catch (KettleException e) {

        System.out.println(e);

      }

   }

}
Copy after login

最后调用代码如下:

publicclass EtlTest {

   publicstaticvoid main(String[] args) {

      KettleUtil2 etl = new KettleUtil2("testimport.ktr");

      Map<String,String> para = new HashMap<String,String>();

      //给转换中命名参数赋值

      para.put("XlsName", "data");

      etl.runTransformation(para);

   }

}
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!