javakettle
① java 怎么设置kettle数据库
java调用kettle数据库类型资源库中的ktr
此问题在1个月前或许已经接触,单是一直木有怎么用到,就被耽搁至今;问题的解决要来源于网络,其实我还想说问题的解决更多的是要靠我们自己的思想,不过多的言情,我们接下来直接进入主题吧!
环境:kettle-spoon 4.2.0,oracle11g,myeclipse6.5,sqlserver2008
前提:在kettle图形界面spoon里面已经做好了一个ktr转换模型,此时我的ktr信息如下图:
Step1:在myeclipse创建project,导入kettle集成所需要的包
Step2:重点解析与code源码
//定义ktr名字
private static String transName = "test1";
//初始化kettle环境
KettleEnvironment.init();
//创建资源库对象,此时的对象还是一个空对象
KettleDatabaseRepository repository = new KettleDatabaseRepository();
//创建资源库数据库对象,类似我们在spoon里面创建资源库
DatabaseMeta dataMeta =
new DatabaseMeta("enfo_bi","Oracle","Native","ip","sid","port","username","password");
//资源库元对象,名称参数,id参数,描述等可以随便定义
KettleDatabaseRepositoryMeta kettleDatabaseMeta =
new KettleDatabaseRepositoryMeta("enfo_bi", "enfo_bi", "king description",dataMeta);
//给资源库赋值
repository.init(kettleDatabaseMeta);
//连接资源库
repository.connect("admin","admin");
//根据变量查找到模型所在的目录对象
RepositoryDirectoryInterface directory = repository.findDirectory("/enfo_worker/wxj");
//创建ktr元对象
TransMeta transformationMeta = ((Repository) repository).loadTransformation(transName, directory, null, true, null ) ;
//创建ktr
Trans trans = new Trans(transformationMeta);
//执行ktr
trans.execute(null);
//等待执行完毕
trans.waitUntilFinished();
上面的两个步骤才可以确定是资源库中的那个路径下的ktr和我们用命令执行一样的-dir ,-tran -job
附上源码:
package kettle;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
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;
/**
* <p>Title: java调用kettle4.2数据库型资料库中的转换</p>
* <p>Description: </p>
* <p>Copyright: Copyright () 2012</p>
*/
public class ExecuteDataBaseRepTran {
private static String transName = "test1";
public static void main(String[] args) {
try {
//初始化kettle环境
KettleEnvironment.init();
//创建资源库对象,此时的对象还是一个空对象
KettleDatabaseRepository repository = new KettleDatabaseRepository();
//创建资源库数据库对象,类似我们在spoon里面创建资源库
DatabaseMeta dataMeta =
new DatabaseMeta("enfo_bi","Oracle","Native","ip","sid","port","username","password");
//资源库元对象,名称参数,id参数,描述等可以随便定义
KettleDatabaseRepositoryMeta kettleDatabaseMeta =
new KettleDatabaseRepositoryMeta("enfo_bi", "enfo_bi", "king description",dataMeta);
//给资源库赋值
repository.init(kettleDatabaseMeta);
//连接资源库
repository.connect("admin","admin");
//根据变量查找到模型所在的目录对象,此步骤很重要。
RepositoryDirectoryInterface directory = repository.findDirectory("/enfo_worker/wxj");
//创建ktr元对象
TransMeta transformationMeta = ((Repository) repository).loadTransformation(transName, directory, null, true, null ) ;
//创建ktr
Trans trans = new Trans(transformationMeta);
//执行ktr
trans.execute(null);
//等待执行完毕
trans.waitUntilFinished();
if(trans.getErrors()>0)
{
System.err.println("Transformation run Failure!");
}
else
{
System.out.println("Transformation run successfully!");
}
} catch (KettleException e) {
e.printStackTrace();
}
}
}
② 如何将kettle 集成到java应用
在Java应用程序中调用Kettle的Transformation
package com.ggd543.kettle.trans
import org.pentaho.di.core.util.EnvUtil
import org.pentaho.di.core.KettleEnvironment
import org.pentaho.di.trans.{Trans, TransMeta}
/**
*
* User: 刘永健
* Date: 12-3-8
* Time: 下午12:14
* To change this template use File | Settings | File Templates.
*/
object TransDemo extends App {
execTrans(args(0)) // ktr文件的全路径
def execTrans(fileName: String) {
KettleEnvironment.init()
EnvUtil.environmentInit();
val transMeta = new TransMeta(fileName)
val trans = new Trans(transMeta)
trans.execute(null) // you can pass arguments instead of null
trans.waitUntilFinished();
if (trans.getErrors > 0) {
throw new RuntimeException("There were errors ring transformation execution")
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>kettledemo</groupId>
<artifactId>kettledemo</artifactId>
<version>1.0</version>
<dependencies>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scala-tools.testing</groupId>
<artifactId>specs_2.9.1</artifactId>
<version>1.6.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.9.1</artifactId>
<version>1.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-testkit</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>pentaho</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<pentaho.kettle.version>4.2.1.1</pentaho.kettle.version>
</properties>
<dependencies>
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-core</artifactId>
<version>${pentaho.kettle.version}</version>
</dependency>
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-db</artifactId>
<version>${pentaho.kettle.version}</version>
</dependency>
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-engine</artifactId>
<version>${pentaho.kettle.version}</version>
</dependency>
<dependency>
<groupId>pentaho</groupId>
<artifactId>pentaho-hdfs-vfs</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-test</artifactId>
<version>${pentaho.kettle.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>pentaho</id>
<name>Pentaho Repository</name>
<url>http://repo.pentaho.org/artifactory/pentaho/</url>
</repository>
</repositories>
</profile>
<profile>
<id>scala</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<scala.version>2.9.1</scala.version>
</properties>
<repositories>
<repository>
<id>typesafe</id>
<name>Typesafe Repository</name>
<url>http://repo.typesafe.com/typesafe/releases/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<version>${scala.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-swing</artifactId>
<version>${scala.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
③ java 调用kettle报错
Unable to get VFS File object for filename 'file:///D:/tDataWarehouseMonitor/wuzilb.ktr' : Unknown scheme "sftp" in URI "{1}".这个错误是没有找到文件的路径,看看这路径下有没有文件
④ java执行kettle5.0 资源库需要哪些jar
学Java的人经常遇到的一个问题是:如果一个程序依赖某个文件夹下的一堆jar包,那么启动它的时候就需要在java
-cp参数后面一个一个的加上jar包的名称,很不方便。
比如主程序类叫Main,在目录lib下有aaa.jar,bbb.jar,ccc.jar,则需要输入以下命令...
⑤ java调用kettle的一个问题
要引用kettle 里lib 表里里的js包,包名名称基本有js相关字样你可以尝试找找,还有一些其它依赖包
⑥ Java获取kettle作业的结果集
result.getrows不是获取trans流程里面‘流动’的数据的。
trans里面有个‘复制记录到结果’插件,输出到里面的数据,通过result.getrows就能获取。
⑦ java调用kettle怎么读取excel文件写入到数据库
解析 excel 用poi apache面架包JDBC 或者数据库框架直接操作数据库案 . 案二 找ETL工具 应该步骤 实现面程像叫 kettle . 用继续网络吧
⑧ java调用kettle文件需要哪些参数
如下为Java调用本地的转换文件,其中String[] params就是参数,示例传递了两个参数:“123”, “234”,传递的参数全部是字符串类型,使用时需要转换成具体的格式:
[java] view plain
/**
* 调用本地的转换文件(带参数)
*
* @Description:
* @param transFileName
* @throws KettleException
* @author 李文锴
* @since:2012-8-15 下午02:58:54
*/
public static void callNativeTransWithParam(String transFileName) throws KettleException {
// 初始化
EnvUtil.environmentInit();
StepLoader.init();
// 转换元对象
TransMeta transMeta = new TransMeta(transFileName);
// 转换
Trans trans = new Trans(transMeta);
String[] params = {"123", "234"}; // 传递参数
// 执行转换
trans.execute(params);
// 等待转换执行结束
trans.waitUntilFinished();
}
⑨ 怎么实现java简单调用kettle我自己做好的job
/**
* 本测试类慎用!!!!!!!
*
* @param args
*/
public static void main(String[] args) {
String datetime = "2014-12-19 23:20:45";
String[] params = {"707", datetime}; // 传递参数
String path = "F:\\job7.kjb";
// runTransfer(params, path);
runJob(params, path);
// runJob();
// jbResource();
}
/**
* 运行转换文件方法
* @param params 多个参数变量值
* @param ktrPath 转换文件的路径,后缀ktr
*/
public static void runTransfer(String[] params, String ktrPath) {
Trans trans = null;
try {
// // 初始化
// 转换元对象
KettleEnvironment.init();// 初始化
EnvUtil.environmentInit();
TransMeta transMeta = new TransMeta(ktrPath);
// 转换
trans = new Trans(transMeta);
// 执行转换
trans.execute(params);
// 等待转换执行结束
trans.waitUntilFinished();
// 抛出异常
if (trans.getErrors() > 0) {
throw new Exception(
"There are errors ring transformation exception!(传输过程中发生异常)");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* java 调用 kettle 的job
*
* @param jobname
* 如: String fName= "D:\\kettle\\informix_to_am_4.ktr";
*/
public static void runJob(String[] params, String jobPath) {
try {
KettleEnvironment.init();
// jobname 是Job脚本的路径及名称
JobMeta jobMeta = new JobMeta(jobPath, null);
Job job = new Job(null, jobMeta);
// 向Job 脚本传递参数,脚本中获取参数值:${参数名}
// job.setVariable(paraname, paravalue);
job.setVariable("id", params[0]);
job.setVariable("dt", params[1]);
job.start();
job.waitUntilFinished();
if (job.getErrors() > 0) {
throw new Exception(
"There are errors ring job exception!(执行job发生异常)");
}
} catch (Exception e) {
e.printStackTrace();
}
}
⑩ java调用kettle时,kettle需要配置环境吗job的路径需要在程序中写出来吗
需要配置环境,job路径不需要在程序中写出来。