使用java调用Kettle资源库中的转换

本帖最后由 mingfly 于 2013-7-9 12:10 编辑

kettle 中配置如下:
  • 创建db资源库:具体参考我的帖子:http://f.dataguru.cn/thread-146141-1-3.html
  • 创建一个简单的转换(生成固定记录保存到文件中)
使用java调用Kettle资源库中的转换_第1张图片 


使用java调用Kettle资源库中的转换_第2张图片

例子使用Maven构建

<properties>
        <pentaho.kettle.version>4.0.1-GA</pentaho.kettle.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>pentaho.kettle</groupId>
            <artifactId>kettle-core</artifactId>
            <version>${pentaho.kettle.version}</version>
        </dependency>
        <dependency>
            <groupId>pentaho.kettle</groupId>
            <artifactId>kettle-db</artifactId>
            <version>${pentaho.kettle.version}</version>
        </dependency>
        <dependency>
            <groupId>pentaho.kettle</groupId>
            <artifactId>kettle-engine</artifactId>
            <version>${pentaho.kettle.version}</version>
        </dependency>
        <dependency>
            <groupId>pentaho.kettle</groupId>
            <artifactId>kettle-ui-swt</artifactId>
            <version>${pentaho.kettle.version}</version>
        </dependency>
        <dependency>
            <groupId>pentaho-library</groupId>
            <artifactId>libformula</artifactId>
            <version>1.1.7</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.janino</groupId>
            <artifactId>janino</artifactId>
            <version>2.5.16</version>
        </dependency>
        <dependency>
            <groupId>rhino</groupId>
            <artifactId>js</artifactId>
            <version>1.7R2</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.1</version>
        </dependency>
        <!-- Assuming the tranformation is stored in MySql database-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.17</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>com.apache.commons</groupId>
            <artifactId>kettle-vfs-20100924</artifactId>
            <version>20100924</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
    </dependencies>
这里特别要注意com.apache.commons的kettle-vfs-20100924 这个要手动加入,之前在公网的Maven Reponsitory找不到相应的包。kettle-vfs-20100924.jar存在{path}\data-integration\libext\pentaho中。
使用java调用Kettle资源库中的转换_第3张图片 

环境配置好之后开始撰写Java代码:
package com.zhuheng.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.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;

/**
 * User: mingfly
 * Mail:[email protected]
 * Date: 13-7-9
 * Time: 上午10:11
 */
public class KettleMain {
    public static void main(String[] args) throws KettleException {
        //初始化环境
        KettleEnvironment.init();
        //创建DB资源库
        KettleDatabaseRepository repository=new KettleDatabaseRepository();
        DatabaseMeta databaseMeta=new DatabaseMeta("Kettle_MySQL","mysql","jdbc","localhost","kettle","3306","root","root");
        //选择资源库
        KettleDatabaseRepositoryMeta kettleDatabaseRepositoryMeta=new KettleDatabaseRepositoryMeta("Kettle","Kettle","Transformation description",databaseMeta);
        repository.init(kettleDatabaseRepositoryMeta);
        //连接资源库
        repository.connect("admin","admin");
        RepositoryDirectoryInterface directoryInterface=repository.loadRepositoryDirectoryTree();
        //选择转换
        TransMeta transMeta=repository.loadTransformation("test",directoryInterface,null,true,null);
        Trans trans=new Trans(transMeta);
        trans.execute(null);
        trans.waitUntilFinished();//等待直到数据结束
        if(trans.getErrors()>0){
            System.out.println("transformation error");
        }else{
            System.out.println("transformation successfully");
        }
    }
}
运行结果

使用java调用Kettle资源库中的转换_第4张图片

使用java调用Kettle资源库中的转换_第5张图片


你可能感兴趣的:(java,maven,Kettle)