Java调用Kettle4.2步骤

1.项目目录结构图

2.源代码

  
  
  
  
  1. package com.winning.kettle; 
  2.  
  3. import org.pentaho.di.core.KettleEnvironment; 
  4. import org.pentaho.di.core.exception.KettleException; 
  5. import org.pentaho.di.job.Job; 
  6. import org.pentaho.di.job.JobMeta; 
  7. import org.pentaho.di.trans.Trans; 
  8. import org.pentaho.di.trans.TransMeta; 
  9.  
  10. public class Transformation 
  11.     public void runTran(String filename) {   
  12.         try {   
  13.             KettleEnvironment.init();   
  14.             TransMeta transMeta = new TransMeta(filename);   
  15.             Trans trans = new Trans(transMeta);   
  16.             //此处为输入的参数,也可以通过参数传进方法中 
  17.             String [] s = {"我是参数1","我是参数2"}; 
  18.             trans.prepareExecution(s);   
  19.             trans.startThreads();   
  20.             trans.waitUntilFinished();   
  21.             if (trans.getErrors() != 0) {   
  22.                 System.out.println("Error");   
  23.             }   
  24.         } catch (KettleException e) {   
  25.             e.printStackTrace();   
  26.         }   
  27.     }   
  28.      
  29.     public void runJob(String jobname){   
  30.           try {   
  31.            KettleEnvironment.init();   
  32.            //jobname 是Job脚本的路径及名称   
  33.            JobMeta jobMeta = new JobMeta(jobname, null);   
  34.            Job job = new Job(null, jobMeta);   
  35.            //向Job 脚本传递参数,脚本中获取参数值:${参数名}   
  36.            //job.setVariable(paraname, paravalue);   
  37.            job.start();   
  38.            job.waitUntilFinished();   
  39.            if (job.getErrors() > 0) {   
  40.             System.out.println("decompress fail!");   
  41.            }   
  42.           } catch (KettleException e) {   
  43.            System.out.println(e);   
  44.           }   
  45.      } 
  46.      
  47.     public static void main(String[] args) 
  48.     { 
  49.         Transformation transformation=new Transformation(); 
  50.         transformation.runTran(System.getProperty("user.dir")+"\\src\\转换1.ktr"); 
  51.         transformation.runJob("C:\\Users\\Administrator\\Desktop\\SendMail.kjb"); 
  52.          
  53.         /* 控制台输出 
  54.             INFO  22-03 21:38:43,981 - 转换1 - 为了转换解除补丁开始  [转换1] 
  55.             INFO  22-03 21:38:44,002 - 获取系统信息 - 完成处理 (I=0, O=0, R=1, W=1, U=0, E=0 
  56.             INFO  22-03 21:38:44,007 - 文本文件输出 - 完成处理 (I=0, O=2, R=1, W=1, U=0, E=0 
  57.             INFO  22-03 21:38:44,044 - SendMail - 开始执行任务 
  58.             INFO  22-03 21:38:44,048 - SendMail - 开始项[Mail] 
  59.             INFO  22-03 21:38:46,890 - SendMail - 完成作业项[Mail] (结果=[true]) 
  60.             INFO  22-03 21:38:46,890 - SendMail - 任务执行完毕 
  61.          */ 
  62.     } 
  63. }

项目例子下载地址(由于这里上传的文件大小有限制):http://down.51cto.com/data/719492

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