Java調(diào)用Kettle執(zhí)行任務(wù)或轉(zhuǎn)換,需要使用Kettle中的jar,,可以先導(dǎo)入lib目錄中的幾個基本的jar,,如:kettle-core.jar、kettle-db.jar,、kettle-engine.jar ,,其它jar根據(jù)情況進(jìn)行添加,所需的jar在<kettle-home>\lib,、<kettle-home>\libext下面都可以找到,,本示例引用的jar如下圖: 之后編寫代碼測試Java調(diào)用,調(diào)用前先使用Kettle的設(shè)計器設(shè)計了一個轉(zhuǎn)換,,取名為voucher.ktr,。另外,本示例使用的是Kettle3.2的版本,,據(jù)說4.1版本調(diào)用方法會有不同。 - import org.pentaho.di.core.database.DatabaseMeta;
- import org.pentaho.di.core.exception.KettleException;
- import org.pentaho.di.core.logging.LogWriter;
- import org.pentaho.di.core.util.EnvUtil;
- import org.pentaho.di.job.Job;
- import org.pentaho.di.job.JobEntryLoader;
- import org.pentaho.di.job.JobMeta;
- import org.pentaho.di.repository.Repository;
- import org.pentaho.di.repository.RepositoryDirectory;
- import org.pentaho.di.repository.RepositoryMeta;
- import org.pentaho.di.repository.UserInfo;
- import org.pentaho.di.trans.StepLoader;
- import org.pentaho.di.trans.Trans;
- import org.pentaho.di.trans.TransMeta;
-
-
-
-
-
-
-
-
- public class KettleTest {
-
- public static void main(String[] args) throws KettleException {
- String filename = "voucher.ktr";
-
-
-
- callNativeTransWithParam(filename);
- System.out.println("ok");
- }
-
-
-
-
-
-
-
-
-
-
- public static void callNativeTrans(String transFileName) throws KettleException {
-
- EnvUtil.environmentInit();
- StepLoader.init();
-
- TransMeta transMeta = new TransMeta(transFileName);
-
- Trans trans = new Trans(transMeta);
-
- trans.execute(null);
-
- trans.waitUntilFinished();
- }
-
-
-
-
-
-
-
-
-
-
- public static void callNativeTransWithParam(String transFileName) throws KettleException {
-
- EnvUtil.environmentInit();
- StepLoader.init();
-
- TransMeta transMeta = new TransMeta(transFileName);
-
- Trans trans = new Trans(transMeta);
- String[] params = {};
-
- trans.execute(params);
-
- trans.waitUntilFinished();
-
- }
-
-
-
-
-
-
-
-
-
- public static void executeTrans() throws KettleException {
-
- EnvUtil.environmentInit();
- StepLoader.init();
-
- LogWriter log = LogWriter.getInstance("TransTest.log", true, LogWriter.LOG_LEVEL_DEBUG);
-
- UserInfo userInfo = new UserInfo();
- userInfo.setLogin("admin");
- userInfo.setPassword("admin");
-
- DatabaseMeta connection = new DatabaseMeta("", "Oracle", "Native", "192.168.3.232", "NSDEV", "1521", "nstcsa3441", "671468");
-
- RepositoryMeta repinfo = new RepositoryMeta();
- repinfo.setConnection(connection);
-
- Repository rep = new Repository(log, repinfo, userInfo);
-
- rep.connect("");
-
- RepositoryDirectory dir = new RepositoryDirectory(rep);
-
- TransMeta transMeta = new TransMeta(rep, "憑證(N9->EVC2)", dir);
-
- Trans trans = new Trans(transMeta);
-
- trans.execute(null);
-
- trans.waitUntilFinished();
- }
-
-
-
-
-
-
-
-
-
-
- public static void callNativeJob(String jobFileName) throws KettleException {
-
- EnvUtil.environmentInit();
- JobEntryLoader.init();
- StepLoader.init();
-
- LogWriter log = LogWriter.getInstance("TransTest.log", true, LogWriter.LOG_LEVEL_DETAILED);
-
- JobMeta jobMeta = new JobMeta(log, jobFileName, null);
-
- Job job = new Job(log, StepLoader.getInstance(), null, jobMeta);
- jobMeta.setInternalKettleVariables(job);
-
- job.execute();
-
- job.waitUntilFinished();
- }
-
-
-
-
-
-
-
-
-
- public static void executeJobs() throws KettleException {
-
- EnvUtil.environmentInit();
- JobEntryLoader.init();
- StepLoader.init();
-
- LogWriter log = LogWriter.getInstance("TransTest.log", true, LogWriter.LOG_LEVEL_DETAILED);
-
- UserInfo userInfo = new UserInfo();
- userInfo.setLogin("admin");
- userInfo.setPassword("admin");
-
- DatabaseMeta connection = new DatabaseMeta("", "Oracle", "Native", "192.168.3.232", "NSDEV", "1521", "nstcsa3441", "671468");
-
- RepositoryMeta repinfo = new RepositoryMeta();
- repinfo.setConnection(connection);
-
- Repository rep = new Repository(log, repinfo, userInfo);
-
- rep.connect("");
-
- RepositoryDirectory dir = new RepositoryDirectory(rep);
-
- StepLoader steploader = StepLoader.getInstance();
-
- JobMeta jobmeta = new JobMeta(log, rep, "4.賬戶每日余額", dir);
-
- Job job = new Job(log, steploader, rep, jobmeta);
-
- job.execute();
-
- job.waitUntilFinished();
-
- }
-
- }
|