Java 加载kjb文件或者ktr文件
1.需要必要的Jar包
kettle-core
kettle-engine
commons-vfs
commons-vfs2
metastore
2.加载工具类如下:
package com.china.job.utils; import org.pentaho.di.core.exception.KettleException; import org.pentaho.di.core.logging.KettleLogStore; import org.pentaho.di.job.Job; import org.pentaho.di.job.JobMeta; import org.pentaho.di.repository.ObjectId; import org.pentaho.di.repository.RepositoryDirectory; import org.pentaho.di.repository.filerep.KettleFileRepository; import org.pentaho.di.repository.kdr.KettleDatabaseRepository; import org.pentaho.di.trans.Trans; import org.pentaho.di.trans.TransMeta; /** * Created by Cunxp on 2017/10/26 16:14 */ public class LoadTaskUtil { /** * 从资源库加载job * @return */ public static Job loadJobFromDB(String id_job) throws KettleException{ Job job=null; KettleDatabaseRepository rep = ConnectUtil.getInstance().getDbConnect(); ObjectId objectId=new ObjectId() { @Override public String getId() { return id_job; } }; JobMeta jobMeta=null; jobMeta = rep.loadJob(objectId, null); job=new Job(rep,jobMeta); job.setStopped(true); return job; } /** * 从文件资源库加载job * @return */ public static Job loadJobFromFile(String fileJobName) throws KettleException{ Job job=null; KettleFileRepository rep = ConnectUtil.getInstance().getFileConnect(); JobMeta jobMeta=null; RepositoryDirectory rd=new RepositoryDirectory(); rd.setName("hehe"); jobMeta = rep.loadJob(rep.getJobId(fileJobName,rd),null); job=new Job(rep,jobMeta); job.setStopped(true); return job; } /** * 从资源库加载trans * @return */ public static Trans loadTransFromDB(String id_transformation) throws KettleException{ Trans trans=null; KettleDatabaseRepository rep = ConnectUtil.getInstance().getDbConnect(); ObjectId objectId=new ObjectId() { @Override public String getId() { return id_transformation; } }; TransMeta transMeta=null; transMeta = rep.loadTransformation(objectId, null); trans=new Trans(transMeta); return trans; } /** * 从文件资源库加载trans
* fileTransName 文件名或者 带相对路径的(指定的baseDir的相对) * @return */ public static Trans loadTransFromFile(String fileTransName) throws KettleException{ Trans trans=null; KettleFileRepository rep = ConnectUtil.getInstance().getFileConnect(); TransMeta transMeta=null; transMeta = rep.loadTransformation(rep.getTransformationID(fileTransName,rep.getUserHomeDirectory()),null); trans=new Trans(transMeta); return trans; } public static void main(String[] args) { try { Job job = loadJobFromFile("file_hello"); job.start(); job.waitUntilFinished(); //日志 System.out.println(KettleLogStore.getAppender().getBuffer( job.getLogChannel().getLogChannelId(), false).toString()); } catch (KettleException e) { e.printStackTrace(); } } }
ConnectUtil代码如下
1 package com.china.datasynchronization.utils; 2 3 import org.junit.runner.notification.RunListener.ThreadSafe; 4 import org.pentaho.di.core.KettleEnvironment; 5 import org.pentaho.di.core.database.DatabaseMeta; 6 import org.pentaho.di.core.exception.KettleException; 7 import org.pentaho.di.repository.filerep.KettleFileRepository; 8 import org.pentaho.di.repository.filerep.KettleFileRepositoryMeta; 9 import org.pentaho.di.repository.kdr.KettleDatabaseRepository; 10 import org.pentaho.di.repository.kdr.KettleDatabaseRepositoryMeta; 11 import org.slf4j.Logger; 12 import top.wboost.common.log.util.LoggerUtil; 13 import top.wboost.common.utils.web.utils.PropertiesUtil; 14 15 /** 16 * Created by Cunxp on 2017/10/10 19:38 17 */ 18 @ThreadSafe 19 public class ConnectUtil { 20 21 private static Logger log = LoggerUtil.getLogger(ConnectUtil.class); 22 23 private ConnectUtil() { 24 } 25 26 private static ConnectUtil instance; 27 28 /** 29 * 资源库 相关 其中rep_dir是必须的 30 */ 31 private String baseDir = PropertiesUtil.getProperty("rep.dir"); 32 private String repName = PropertiesUtil.getProperty("rep.name"); 33 private String repDescription = PropertiesUtil.getProperty("rep.description"); 34 private String connectName = PropertiesUtil.getProperty("rep.connect.name"); 35 /** 36 * 远程服务目录l 37 */ 38 public String serverKtrPath = PropertiesUtil.getProperty("servers.ktr.path"); 39 public String serverShPath = PropertiesUtil.getProperty("servers.sh.path"); 40 41 42 public synchronized static ConnectUtil getInstance() { 43 if (instance == null) { 44 instance = new ConnectUtil(); 45 } 46 return instance; 47 } 48 49 /** 50 * 返回数据库型资源库连接 51 */ 52 public KettleDatabaseRepository getDbConnect() { 53 String connectType = PropertiesUtil.getProperty("rep.connect.type"); 54 String connectRemNat = PropertiesUtil.getProperty("rep.connect.remnat"); 55 String connectHost = PropertiesUtil.getProperty("rep.connect.host"); 56 String connectDb = PropertiesUtil.getProperty("rep.connect.db"); 57 String connectPort = PropertiesUtil.getProperty("rep.connect.port"); 58 String connectUser = PropertiesUtil.getProperty("rep.connect.user"); 59 String connectPass = PropertiesUtil.getProperty("rep.connect.pass"); 60 try { 61 KettleEnvironment.init(); 62 } catch (KettleException e) { 63 log.error("kettle初始化失败!{}", e); 64 } 65 String repUser = PropertiesUtil.getProperty("rep.user"); 66 String repPass = PropertiesUtil.getProperty("rep.pass"); 67 KettleDatabaseRepository repository = new KettleDatabaseRepository(); 68 DatabaseMeta dataMeta = new DatabaseMeta(connectName, connectType, 69 connectRemNat, connectHost, connectDb, 70 connectPort, connectUser, connectPass); 71 KettleDatabaseRepositoryMeta kettleDatabaseMeta = new KettleDatabaseRepositoryMeta( 72 repName, connectName, repDescription, dataMeta); 73 repository.init(kettleDatabaseMeta); 74 try { 75 repository.connect(repUser, repPass); 76 } catch (KettleException e) { 77 log.error("建立资源库连接失败!{}", e); 78 } 79 return repository; 80 } 81 82 /** 83 * 返回文件型资源库连接 84 */ 85 public KettleFileRepository getFileConnect() { 86 try { 87 KettleEnvironment.init(); 88 } catch (KettleException e) { 89 log.error("kettle初始化失败!{}", e); 90 } 91 KettleFileRepository repository = new KettleFileRepository(); 92 KettleFileRepositoryMeta kettleFileRepositoryMeta = new KettleFileRepositoryMeta( 93 repName, connectName, repDescription, baseDir); 94 repository.init(kettleFileRepositoryMeta); 95 try { 96 repository.connect(null, null); 97 } catch (KettleException e) { 98 log.error("建立资源库连接失败!{}", e); 99 } 100 return repository; 101 } 102 103 public String getbaseDir() { 104 return baseDir; 105 } 106 107 public String getserverShPath() { 108 return serverShPath; 109 } 110 }