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路徑不需要在程序中寫出來。