java调用Kettle5.3的任务

</pre><pre name="code" class="html">package com.test;

import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.job.Job;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.repository.Repository;
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数据库型资料库中的任务
 * 
 * @author 
 * 
 */
public class JobTest {

	private static String transName = "Main.job";

	/**
	 * @param args
	 * @throws KettleException
	 */
	public static void main(String[] args) throws KettleException {
		try {
			KettleEnvironment.init();
			KettleDatabaseRepository repository = new KettleDatabaseRepository();
			DatabaseMeta dataMeta = new DatabaseMeta("enfo_bi", "Mysql",
					"Native", "127.0.0.1", "kettle", "3306", "root", "");
			KettleDatabaseRepositoryMeta kettleDatabaseMeta = new KettleDatabaseRepositoryMeta(
					"enfo_bi", "enfo_bi", "king description", dataMeta);
			repository.init(kettleDatabaseMeta);
			repository.connect("admin", "admin");
			RepositoryDirectoryInterface directory = repository.findDirectory("/");
			String excuteType = transName.substring(transName.lastIndexOf(".")+1,transName.length());
			transName = transName.substring(0,transName.lastIndexOf("."));
			
			if (excuteType.equals("job")) {
				JobMeta meta = ((Repository) repository).loadJob(transName,
						directory, null, null);
				Job job = new Job(repository, meta);
				job.run();
				job.waitUntilFinished();
				if (job.getErrors() > 0) {
					System.err.println(excuteType+":Transformation run Failure!");
				} else {
					System.out.println(excuteType+":Transformation run successfully!");
				}
			} else {
				TransMeta transformationMeta = ((Repository) repository)
						.loadTransformation(transName, directory, null, true,
								null);
				Trans trans = new Trans(transformationMeta);
				trans.execute(null);
				trans.waitUntilFinished();
				if (trans.getErrors() > 0) {
					System.err.println(excuteType+":Transformation run Failure!");
				} else {
					System.out.println(excuteType+":Transformation run successfully!");
				}
			}
		} catch (KettleException e) {
			e.printStackTrace();
		}
	}

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值