package com.mierx.platform.kettle;
import java.util.List;
import org.apache.log4j.Logger;
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;
import com.mierx.core.modules.util.PropertiesLoader;
/**
*
* 交换工具
*
* @author gaochunhu
* @Date: 2018-9-25 下午02:15:18
* @version 3.5
* @Company: mierx
* @Copyright: Copyright © 2018 Shaanxi Tiancheng Software Co.,Ltd
*/
public class KettleUtils
{
/**
* 日志记录
*/
private static Logger logger = Logger.getLogger(KettleUtils.class);
private static final String kettleId = "kettle_resource";
private static final String kettleName = "kettle_resource";
private static final String kettleDes = "kettle_resource";
/**
* 获取数据资源库连接
*/
private static DatabaseMeta databaseMeta = null;
private static KettleDatabaseRepository repository = null;
/**
* 初始化连接数据资源库
*
* @Description:
*/
protected static DatabaseMeta getDatabaseMeta()
{
if (null == databaseMeta)
{
PropertiesLoader prop = new PropertiesLoader("application.properties");
databaseMeta =
new DatabaseMeta("mysql", "mysql", "jdbc", prop.getProperty("host", "").trim(), prop.getProperty("db", "swtdexchang?characterEncoding=utf8")
.trim(), prop.getProperty("port", "3306").trim(), prop.getProperty("user", "root").trim(), prop.getProperty("pass", "mysql").trim());
}
return databaseMeta;
}
/**
* 进行资源初始化
*
* @throws KettleException
* @Description:
*/
private static void init()
throws KettleException
{
// 初始化环境
KettleEnvironment.init();
// 创建DB资源库
repository = new KettleDatabaseRepository(); // 报异常,说一个类找不到
DatabaseMeta databaseMeta = getDatabaseMeta();
// 选择资源库
KettleDatabaseRepositoryMeta kettleDatabaseRepositoryMeta = new KettleDatabaseRepositoryMeta(kettleId, kettleName, kettleDes, databaseMeta);
repository.init(kettleDatabaseRepositoryMeta);
// 连接资源库
repository.connect("admin", "admin");
}
protected static List<String> findAllJobs()
throws KettleException
{
// 初始化资源库信息
init();
// 查找作业并进行作业计划
return null;
}
/**
* 启动执行资源库中的作业
*
* @param jobName 执行作业的名称
* @param jobPath 作业所在路径
* @throws KettleException
* @Description:
*/
protected static void runRepJob(final String jobName, final String jobPath)
throws KettleException
{
// 初始化资源库信息
init();
// 查找作业并进行作业计划
RepositoryDirectoryInterface directory = repository.findDirectory(jobPath);
JobMeta jobMetaRep = ((Repository)repository).loadJob(jobName, directory, null, null);
Job job = new Job(repository, jobMetaRep);
job.start();
job.waitUntilFinished();
if (job.getErrors() > 0)
{
logger.error(String.format("%s run Failure!", jobPath + "/" + jobName));
}
else
{
logger.info(String.format("%s run successfully!", jobPath + "/" + jobName));
}
repository.disconnect();
}
protected static void runRepTransform(final String transName, final String transPath)
throws KettleException
{
// 初始化资源库信息
init();
// 查找转换并进行转化操作
RepositoryDirectoryInterface directoryInterface = repository.findDirectory(transPath);
// 选择转换
TransMeta transMeta = repository.loadTransformation(transName, directoryInterface, null, true, null);
Trans trans = new Trans(transMeta);
trans.execute(null);
trans.waitUntilFinished();// 等待直到数据结束
if (trans.getErrors() > 0)
{
logger.error(String.format("%s run Failure!", transPath + "/" + transName));
}
else
{
logger.info(String.format("%s run successfully!", transPath + "/" + transName));
}
repository.disconnect();
}
// public static void main(String[] args)
// throws KettleException
// {
// String jobName = "商务厅数据交换总入口";
// String jobPath = "/商务厅数据交换";
// runRepJob(jobName, jobPath);
// String transName = "招商引资_省内实施项目表";
// String transPath = "/商务厅数据交换/陕西省招商引资系统/省内实施项目";
// runRepTransform(transName, transPath);
//
// }
}
java调用执行转换和作业
最新推荐文章于 2024-04-23 13:01:43 发布