本文目錄一覽:
如何將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 during transformation execution”)
}
}
}
?xml version=”1.0″ encoding=”UTF-8″?
project xmlns=””
xmlns:xsi=””
xsi:schemaLocation=” “
modelVersion4.0.0/modelVersion
groupIdkettledemo/groupId
artifactIdkettledemo/artifactId
version1.0/version
dependencies
!– Test —
dependency
groupIdjunit/groupId
artifactIdjunit/artifactId
version4.8.1/version
scopetest/scope
/dependency
dependency
groupIdorg.scala-tools.testing/groupId
artifactIdspecs_2.9.1/artifactId
version1.6.9/version
scopetest/scope
/dependency
dependency
groupIdorg.scalatest/groupId
artifactIdscalatest_2.9.1/artifactId
version1.6.1/version
scopetest/scope
/dependency
dependency
groupIdcom.typesafe.akka/groupId
artifactIdakka-actor/artifactId
version2.0/version
/dependency
dependency
groupIdcom.typesafe.akka/groupId
artifactIdakka-testkit/artifactId
version2.0/version
/dependency
/dependencies
build
plugins
plugin
groupIdorg.apache.maven.plugins/groupId
artifactIdmaven-compiler-plugin/artifactId
configuration
source1.6/source
target1.6/target
encodingUTF-8/encoding
/configuration
/plugin
/plugins
/build
profiles
profile
idpentaho/id
activation
activeByDefaulttrue/activeByDefault
/activation
properties
pentaho.kettle.version4.2.1.1/pentaho.kettle.version
/properties
dependencies
dependency
groupIdpentaho-kettle/groupId
artifactIdkettle-core/artifactId
version${pentaho.kettle.version}/version
/dependency
dependency
groupIdpentaho-kettle/groupId
artifactIdkettle-db/artifactId
version${pentaho.kettle.version}/version
/dependency
dependency
groupIdpentaho-kettle/groupId
artifactIdkettle-engine/artifactId
version${pentaho.kettle.version}/version
/dependency
dependency
groupIdpentaho/groupId
artifactIdpentaho-hdfs-vfs/artifactId
version1.0.1/version
/dependency
dependency
groupIdlog4j/groupId
artifactIdlog4j/artifactId
version1.2.16/version
/dependency
dependency
groupIdpentaho-kettle/groupId
artifactIdkettle-test/artifactId
version${pentaho.kettle.version}/version
scopetest/scope
/dependency
/dependencies
repositories
repository
idpentaho/id
namePentaho Repository/name
url;/url
/repository
/repositories
/profile
profile
idscala/id
activation
activeByDefaulttrue/activeByDefault
/activation
properties
scala.version2.9.1/scala.version
/properties
repositories
repository
idtypesafe/id
nameTypesafe Repository/name
url;/url
/repository
/repositories
dependencies
dependency
groupIdorg.scala-lang/groupId
artifactIdscala-compiler/artifactId
version${scala.version}/version
scopecompile/scope
/dependency
dependency
groupIdorg.scala-lang/groupId
artifactIdscala-library/artifactId
version${scala.version}/version
/dependency
dependency
groupIdorg.scala-lang/groupId
artifactIdscala-swing/artifactId
version${scala.version}/version
/dependency
/dependencies
build
plugins
plugin
groupIdorg.scala-tools/groupId
artifactIdmaven-scala-plugin/artifactId
executions
execution
goals
goalcompile/goal
goaltestCompile/goal
/goals
/execution
/executions
/plugin
/plugins
/build
/profile
/profiles
/project
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;
/**
* pTitle: java調用kettle4.2資料庫型資料庫中的轉換/p
* pDescription: /p
* pCopyright: 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();
}
}
}
如何將JAVA程序集成到KETTLE中
在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 during transformation execution”)
}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/297766.html