java項目執行kettle文件+JNDI連接配置+配置變量

Java執行kettle文件

添加依賴:

<dependency>
    <groupId>pentaho-kettle</groupId>
    <artifactId>kettle-engine</artifactId>
    <version>7.0.0.0-25</version>
</dependency>
<dependency>
    <groupId>pentaho-kettle</groupId>
    <artifactId>kettle-core</artifactId>
    <version>7.0.0.0-25</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.6</version>
</dependency>

工具類

package com.example.kettledemo.util;

import java.io.File;
import java.util.Map;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.util.EnvUtil;
import org.pentaho.di.job.Job;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
/**
 * @Package: com.example.kettledemo.util
 * @Since: 2020/5/13 14:26
 * @Version: V1.0
 */
public class kettleUtil {

        /**
         * 調用trans文件
         *
         * @param transFileName
         * @throws Exception
         */
        public static void callNativeTrans(String transFileName) throws Exception {
            callNativeTransWithParams(null, transFileName);
        }

        /**
         * 調用trans文件 帶參數的
         *
         * @param params
         * @param transFileName
         * @throws Exception
         */
        public static void callNativeTransWithParams(String[] params, String transFileName) throws Exception {
            // 初始化
            KettleEnvironment.init();
            EnvUtil.environmentInit();
            TransMeta transMeta = new TransMeta(transFileName);
            //轉換
            Trans trans = new Trans(transMeta);
            //執行
            trans.execute(params);
            //等待結束
            trans.waitUntilFinished();
            //拋出異常
            if (trans.getErrors() > 0) {
                throw new Exception("There are errors during transformation exception!(傳輸過程中發生異常)");
            }
        }

        /**
         * 調用job文件
         *
         * @param jobName
         * @throws Exception
         */
        public static void callNativeJob(String jobName, Map map) throws Exception {
            // 初始化
            String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
            File file = new File(path+"/simple-jndi");// path是jdbc.prtoperties上層文件夾路徑
            String sysPath = file.getCanonicalPath();
            Const.JNDI_DIRECTORY = sysPath;
            KettleEnvironment.init();

            JobMeta jobMeta = new JobMeta(jobName, null);
            Job job = new Job(null, jobMeta);
            //向Job 腳本傳遞變量,腳本中獲取參數值:${變量名}
            job.injectVariables(map);
            job.start();
            job.waitUntilFinished();
            if (job.getErrors() > 0) {
                throw new Exception("There are errors during job exception!(執行job發生異常)");
            }
        }
}

XXX.kjb中配置的變量:

測試:執行kettle任務

filePath爲kettle任務文件路徑,param爲變量

@GetMapping("test")
public void test(String filePath, String param) throws Exception {
    HashMap<Object, Object> map = Maps.newHashMap();
    map.put("param", param);
    kettleUtil.callNativeJob(filePath, map);
}

 

配置JNDI

1.將JNDI配置文件(data-integration/simple-jndi/jdbc.properties)放在一個指定的目錄下,如項目根目錄下simple-jndi/jdbc.properties:

項目根目錄下simple-jndi/jdbc.properties:

2.添加simple-jndi的jar依賴。

<dependency>
    <groupId>simple-jndi</groupId>
    <artifactId>simple-jndi</artifactId>
    <version>0.11.4.1</version>
</dependency>

3.調用kettle的環境初始化方法KettleEnvironment.init(true),加載JNDI

String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
File file = new File(path+"/simple-jndi");// path是jdbc.prtoperties上層文件夾路徑
String sysPath = file.getCanonicalPath();
Const.JNDI_DIRECTORY = sysPath;
KettleEnvironment.init();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章