原创文章,转载请注明出处:http://qq85609655.iteye.com/blog/1306555
上篇文章中Kettle4 Repository 操作示例 (登陆资源 ...
今天,想将kettle整合到web工程中。这个想法一出现,就马上查找资料,可惜资料太少,没有现成的。
后面查看源码。。。终于整合到web工程中,方法如下:
编写一个Servlet,工程启动时成功,顺利完成集成。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>kettleapi4_2</display-name> <servlet> <servlet-name>KettleStartServlet</servlet-name> <servlet-class>zhangxin.kettle.servlet.KettleStartServlet</servlet-class> <load-on-startup>0</load-on-startup> <init-param> <param-name>dir</param-name> <param-value>/test</param-value> </init-param> <init-param> <param-name>jobname</param-name> <param-value>测试作业</param-value> </init-param> </servlet> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
Servlet代码如下:
package zhangxin.kettle.servlet;
import java.io.File;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.Result;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.plugins.PluginRegistry;
import org.pentaho.di.core.plugins.RepositoryPluginType;
import org.pentaho.di.job.Job;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.repository.ObjectId;
import org.pentaho.di.repository.RepositoriesMeta;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.repository.RepositoryDirectoryInterface;
import org.pentaho.di.repository.RepositoryElementMetaInterface;
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 KettleStartServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 4122742794095256794L;
public void init() throws ServletException {
try {
// 设置Kettle的初始化配置信息路径
initKettleEnvironment();
ServletConfig config=getServletConfig();
//通过 ServletConfig对象获取配置参数:dirverString
String dir = config.getInitParameter("dir");
String jobname = config.getInitParameter("jobname");
executeJob(dir, jobname);
} catch (KettleException e) {
e.printStackTrace();
}
}
/**
* 设置Kettle的初始化配置信息路径
* @throws KettleException
*/
private void initKettleEnvironment() throws KettleException {
// 获得执行类的当前路径
String user_dir = System.getProperty("user.dir");
String kettleHome = this.getServletContext().getRealPath(File.separator + "WEB-INF");
// Kettle初始化需要修改相应的配置路径
System.setProperty("user.dir", kettleHome);
System.setProperty("KETTLE_HOME", kettleHome);
// 运行环境初始化(设置主目录、注册必须的插件等)
KettleEnvironment.init();
// Kettle初始化完毕,还原执行类的当前路径
System.setProperty("user.dir", user_dir);
}
/**
* Kettle执行Job
* @throws KettleException
*/
public void executeJob(String dir, String jobname) throws KettleException {
RepositoriesMeta repositoriesMeta = new RepositoriesMeta();
// 从文件读取登陆过的资源库信息
repositoriesMeta.readData();
// 选择登陆过的资源库
RepositoryMeta repositoryMeta = repositoriesMeta.findRepository("4_2");
// 获得资源库实例
Repository repository = PluginRegistry.getInstance().loadClass(RepositoryPluginType.class, repositoryMeta, Repository.class);
repository.init(repositoryMeta);
// 连接资源库
repository.connect("admin", "admin");
RepositoryDirectoryInterface tree = repository.loadRepositoryDirectoryTree();
RepositoryDirectoryInterface fooBar = tree.findDirectory(dir);
JobMeta jobMeta = repository.loadJob(jobname, fooBar, null, null);
// 执行指定作业
Job job = new Job(repository, jobMeta);
job.start();
job.waitUntilFinished();
Result result = job.getResult();
result.getRows();
if (job.getErrors() > 0) {
throw new RuntimeException("There were errors during transformation execution.");
}
repository.disconnect();
}
}
这样就集成好了。。。
下面是工程的结构
工程中的包和资源如下
libraries=
../lib
../libext
../libext/commons
../libext/elasticsearch
../libext/feeds
../libext/google
../libext/hive
../libext/hl7
../libext/JDBC
../libext/jersey
../libext/jfree
../libext/mondrian
../libext/pentaho
../libext/poi
../libext/reporting
../libext/rules
../libext/salesforce
../libext/spring
../libext/web
../libext/webservices
../libswt
classpath=
../
../ui
../ui/images
../libext/mondrian/config
../libext/pentaho/pigConf
同时,将 kettle启动时自动生成的 .kettle 文件夹
拷贝到 WEB-INF文件夹下(因为文件夹.kettle 前面有个'点号'。造成文件夹没能在上面的工程结构图中显示)