1. Spring Boot 项目中引入 kettle相关依赖

kettle一些相关的依赖无法从maven中央仓库中直接获取,需要从kettle工具中提取。 从kettle的安装路径(例如:D:\Program Files\pdi-ce-8.2.0.0-342\data-integration\lib)中获取以下jar包,放到Spring Boot项目中。
在这里插入图片描述

2. 编写pom.xml

<dependency>
    <groupId>org.pentaho.di</groupId>
    <artifactId>kettle-core</artifactId>
    <version>7.1.0.0-12</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/kettle-core-7.1.0.0-12.jar</systemPath>
</dependency>
<dependency>
    <groupId>org.pentaho.di</groupId>
    <artifactId>kettle-engine</artifactId>
    <version>7.1.0.0-12</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/kettle-engine-7.1.0.0-12.jar</systemPath>
</dependency>
<!-- kettle中javascript脚本使用的jar -->
<dependency>
    <groupId>org.mozilla</groupId>
    <artifactId>javascript</artifactId>
    <version>1.7.2</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/js-1.7R3.jar</systemPath>
</dependency>
<dependency>
    <groupId>org.pentaho.metastore</groupId>
    <artifactId>metastore</artifactId>
    <version>7.1.0.0-12</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/metastore-7.1.0.0-12.jar</systemPath>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-vfs2</artifactId>
    <version>2.1</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/commons-vfs2-2.1-20150824.jar</systemPath>
</dependency>
<dependency>
    <groupId>net.sourceforge</groupId>
    <artifactId>jtds</artifactId>
    <version>1.3.1</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/jtds-1.3.1.jar</systemPath>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.3</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/commons-logging-1.1.3.jar</systemPath>
</dependency>

<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.54</version>
</dependency>
<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>21.0</version>
</dependency>
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.8</version>
</dependency>

以上依赖是结合网上案例并亲自测试后可用支撑起kettle运行的依赖,可能会出现冗余的依赖,请结合自己的实际项目导入具体依赖 。

3. 编写KettleService类

import lombok.extern.slf4j.Slf4j;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.job.Job;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

/**
 * @author NoDeal
 */
@Slf4j
@Service
public class KettleService {
	//ktr、kjb执行文件所在路径
    @Value("${kettle.script.path}")
    private String dirPath;

    /**
     * 执行ktr文件
     * @param filename 文件名
     * @param params 命名参数
     * @param variables 变量
     * @return
     */
    public String runKtr(String filename, Map<String, String> params, Map<String, String> variables) {
        try {
            log.info("开始执行[{}]文件,参数如下:", filename);
            KettleEnvironment.init();
            TransMeta tm = new TransMeta(dirPath + File.separator + filename);
            Trans trans = new Trans(tm);

            if (params != null) {
                for(String key: params.keySet()){
                    log.info("参数名:{}, 参数值: {}", key, params.get(key));
                    trans.setParameterValue(key,params.get(key));
                }
            }
            if (variables != null) {
                for(String key: variables.keySet()){
                    log.info("变量名:{}, 变量值: {}", key, variables.get(key));
                    trans.setVariable(key,variables.get(key));
                }
            }
            trans.execute(null);
            trans.waitUntilFinished();
        } catch (Exception e) {
            log.error("执行[{}]报错,错误原因:{}", filename, e.getMessage(), e);
        }
        return "ok";
    }

    /**
     * 执行kjb文件
     * @param filename 文件名
     * @param params 命名参数
     * @param variables 变量
     * @return
     */
    public String runKjb(String filename, Map<String, String> params, Map<String, String> variables) {
        try {
            KettleEnvironment.init();
            JobMeta jm = new JobMeta(dirPath + File.separator + filename, null);
            Job job = new Job(null, jm);
            if (params != null) {
                for(String key: params.keySet()){
                    log.info("参数名:{}, 参数值: {}", key, params.get(key));
                    job.setParameterValue(key,params.get(key));
                }
            }
            if (variables != null) {
                for(String key: variables.keySet()){
                    log.info("变量名:{}, 变量值: {}", key, variables.get(key));
                    job.setVariable(key,variables.get(key));
                }
            }
            job.start();
            job.waitUntilFinished();
        } catch (Exception e) {
            log.error("执行[{}]报错,错误原因:{}", filename, e.getMessage(), e);
        }
        return "ok";
    }
}

4. 编写业务service和controller

5. 未解决问题

trans中无法接收到job传入的命名参数

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐