Java kettle程序执行 kettle文件

Java执行本地ktr文件和kjb文件

import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.util.EnvUtil;
import org.pentaho.di.job.Job;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.repository.RepositoryDirectoryInterface;
import org.pentaho.di.repository.kdr.KettleDatabaseRepository;
import org.pentaho.di.repository.kdr.KettleDatabaseRepositoryMeta;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;

/**
 * Java调用Kettle示例代码
 */
public class KettleTest {

	public static void main(String[] args) throws KettleException {
		// runTransformation("switch.ktr");
		// runJob("xx.kjb");
		runRepoTransformation("XXXX");
	}

	/**
	 * @author:xxxx
	 * @TODO:跑本地ktr文件
	 */
	@SuppressWarnings("all")
	public static boolean runTransformation(String filename) {
		boolean flag = false;
		try {
			// 初始化任务
			KettleEnvironment.init();
			EnvUtil.environmentInit();
			TransMeta transMeta = new TransMeta(filename);
			transMeta.setCapturingStepPerformanceSnapShots(true);
			Trans trans = new Trans(transMeta);
			trans.setMonitored(true);
			trans.setInitializing(true);
			trans.setPreparing(true);
			trans.setRunning(true);
			trans.setSafeModeEnabled(true);
			// 设置参数 在kettle文件中sql语句的设置 SELECT * FROM xx where xx=${stnlevel}
			// sql参数必须一一对应
			trans.setVariable("stnlevel", "2");
			trans.execute(null);
			// 等待转换执行结束
			trans.waitUntilFinished();
			flag = true;
		} catch (KettleException e) {
			System.out.println(e);
		}
		return flag;
	}

	/**
	 * @author:xxxx
	 * @TODO:跑本地kjb文件
	 */
	@SuppressWarnings("all")
	public static boolean runJob(String filename) {
		boolean flag = false;
		try {
			KettleEnvironment.init();
			// jobname 是Job脚本的路径及名称
			JobMeta jobMeta = new JobMeta(filename, null);
			Job job = new Job(null, jobMeta);
			// job.setVariable("id", params[0]);
			// job.setVariable("dt", params[1]);
			job.start();
			job.waitUntilFinished();
			flag = true;
		} catch (KettleException e) {
			System.out.println(e);
		}
		return flag;
	}

	/**
	 * @author:xxxx
	 * @TODO:跑资源库ktr文件
	 */
	@SuppressWarnings("all")
	public static boolean runRepoTransformation(String filename) {
		boolean flag = false;
		try {
			// 初始化环境
			KettleEnvironment.init();
			// 创建资源库 对象
			KettleDatabaseRepository repository = new KettleDatabaseRepository();
			// 创建资源库所在的数据库
			DatabaseMeta databaseMeta = new DatabaseMeta("",
					"oracle", "jdbc", "192.168.1.254", "ORCL", "1521",
					"cmbexam", "cmbexam");
			// 选择资源库  找到对应的资源库名称
			KettleDatabaseRepositoryMeta kettleDatabaseRepositoryMeta = new KettleDatabaseRepositoryMeta(
					"xx", "xx", "Transformation description",
					databaseMeta);
			repository.init(kettleDatabaseRepositoryMeta);
			// 连接资源库  资源库的用户名和密码
			repository.connect("admin", "1");
			RepositoryDirectoryInterface directoryInterface = repository
					.loadRepositoryDirectoryTree();
			// 选择转换
			TransMeta transMeta = repository.loadTransformation("XXXX",
					directoryInterface, null, true, null);
			Trans trans = new Trans(transMeta);
			trans.setVariable("stnlevel", "2");
			trans.execute(null);
			// 等待直到数据结束
			trans.waitUntilFinished();
			if (trans.getErrors() > 0) {
				System.out.println("transformation error");
			} else {
				System.out.println("transformation successfully");
			}
			flag = true;
		} catch (KettleException e) {
			System.out.println(e);
		}
		return flag;
	}

}




kjb文件中所用到的ktr文件必须与kjb文件放置在一起

注意事项:任务和转换要存成文件格式,任务中引用的转换也要是文件格式,否则就都需要数据库支撑,数据库方法调用了。


你可能感兴趣的:(Kettle)