kettle应用程序集成代码 博客分类: 开源组件 javakettle
程序员文章站
2024-03-22 19:09:10
...
package com.zjhcsoft.kettle.test; import org.pentaho.di.core.KettleEnvironment; import org.pentaho.di.core.exception.KettleException; import org.pentaho.di.core.logging.CentralLogStore; import org.pentaho.di.core.logging.Log4jBufferAppender; import org.pentaho.di.core.plugins.PluginRegistry; import org.pentaho.di.core.plugins.RepositoryPluginType; import org.pentaho.di.core.util.EnvUtil; import org.pentaho.di.repository.ObjectId; import org.pentaho.di.repository.RepositoriesMeta; import org.pentaho.di.repository.Repository; import org.pentaho.di.repository.RepositoryMeta; import org.pentaho.di.repository.StringObjectId; import org.pentaho.di.trans.Trans; import org.pentaho.di.trans.TransMeta; public class TestKettle { /** * 执行转换方法--执行文件类型方式 * @author twr_wjz * @date 2011-11-17 * @param filename 转换文件路径 * @throws Exception */ public void runFileTransformation(String filename) { try { // 运行环境初始化(设置主目录、注册必须的插件等) KettleEnvironment.init(); EnvUtil.environmentInit(); //获取转换 TransMeta transMeta = new TransMeta(filename); Trans trans = new Trans(transMeta); // 执行指定转换You can pass arguments instead of null. trans.execute(null); // 等待转换执行结束 trans.waitUntilFinished(); /* * 抛出异常 */ if ( trans.getErrors() > 0 ) { throw new RuntimeException( "转换执行期间有错误。" ); } } catch ( KettleException e ) { // TODO Put your exception-handling code here. System.out.println(e); } } /** * 执行转换方法--连接资源库方式 * @author twr_wjz * @date 2011-11-17 * @param repID 资源库ID,如:1 ----建立资源库时,填写字段 * @param transformationID 转换ID--对应数据库中表r_transformation的ID_TRANSFORMATION * @param transformationVersion 转换版本--对应数据库中表r_transformation的TRANS_VERSION * @throws Exception */ /** * @param repID * @param transformationID * @param transformationVersion */ public void runRepTransformation(String repID,String transformationID,String transformationVersion) { try { // 运行环境初始化(设置主目录、注册必须的插件等) KettleEnvironment.init(); RepositoriesMeta repositoriesMeta = new RepositoriesMeta(); // 从文件读取登陆过的资源库信息 repositoriesMeta.readData(); // 选择登陆过的资源库 RepositoryMeta repositoryMeta = repositoriesMeta.findRepository(repID); // 获得资源库实例 Repository repository = PluginRegistry.getInstance().loadClass(RepositoryPluginType.class, repositoryMeta.getId(), Repository.class); repository.init(repositoryMeta); // 连接资源库 repository.connect("admin", "admin"); //设置转换ID ObjectId id = new StringObjectId(transformationID); //获取转换 TransMeta transMeta = repository.loadTransformation(id,transformationVersion); Trans trans = new Trans(transMeta); // 执行指定转换You can pass arguments instead of null. trans.execute(null); // 创建打印日志类 Log4jBufferAppender appender = CentralLogStore.getAppender(); // 等待转换执行结束 trans.waitUntilFinished(); //获取转换中的IU步骤 //InsertUpdate insertUpdate = (InsertUpdate)trans.findBaseSteps("IU").get(0); //String logText = appender.getBuffer(insertUpdate.getLogChannelId(), false).toString(); //System.out.println("日志打印开始\n"+logText+"\n打印日志结束。\n"); // 回收打印日志类 // CentralLogStore.discardLines(trans.getLogChannelId(),false); /* * 抛出异常 */ if ( trans.getErrors() > 0 ) { throw new RuntimeException( "转换执行期间有错误。" ); } repository.disconnect(); } catch ( KettleException e ) { // TODO Put your exception-handling code here. System.out.println(e); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub TestKettle testKettle = new TestKettle(); // testKettle.runFileTransformation("C:/111.ktr"); System.out.println(System.getProperty("user.dir")); System.out.println(System.getProperty("KETTLE_HOME")); System.setProperty("user.dir", "E:/myeclipseworkspace/testkettle/kettleconfig"); System.setProperty("KETTLE_HOME", "E:/myeclipseworkspace/testkettle/kettleconfig"); System.out.println(System.getProperty("user.dir")); System.out.println(System.getProperty("KETTLE_HOME")); testKettle.runRepTransformation("testkettle","1","1.0"); testKettle.runRepTransformation("testkettle","2","1.0"); } }
kettle配置目录变更后的代码配置如下