如何将kettle 集成到java应用

如何将kettle 集成到java应用,第1张

在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="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调用本地的转换文件,其中String[] params就是参数,示例传递了两个悉旦型参数:“123”, “234”,传递的参数全部是字符串类睁猜型,使用时需要转换成具体的格式:

[java] view plaincopy

/**

* 调用本地的转换文件(带参数)

*

* @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数据库类型资姿迅袭源库中的ktr 此问迹兄题在1个月昌岩前或许已经接触,单是一直木有怎么用到,就被耽搁至今;问题的解决要来源于网络,其实我还想说问题的解决更多的是要靠我们自己的思想,不过多的言情,我们接下来直接进入主题吧!


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12330808.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存