烦请哪位大神指导下怎么实现java调用kettle

烦请哪位大神指导下怎么实现java调用kettle,第1张

package org.gdh.kettle.transform

import org.pentaho.di.core.KettleEnvironment

import org.pentaho.di.core.exception.KettleException

import org.pentaho.di.core.exception.KettleXMLException

import org.pentaho.di.job.Job

import org.pentaho.di.job.JobMeta

import org.pentaho.di.trans.Trans

import org.pentaho.di.trans.TransMeta

public class ReadJob {

public static void runJob(String jobname){

try {

KettleEnvironment.init()

//jobname 是Job脚本的路径及名称

JobMeta jobMeta = new JobMeta(jobname, null)

Job job = new Job(null, jobMeta)

//向Job 脚本传递参数,脚本中获取参数陵此陪值:${参数名}

// job.setVariable(paraname, paravalue)

job.start()

job.waitUntilFinished()

if (job.getErrors() >0) {

System.out.println("decompress fail!")

}

} catch (KettleException e) {

System.out.println(e)

}

}

// 调用Transformation示例

public static void runTrans(String filename) {

try {

KettleEnvironment.init()

TransMeta transMeta = new TransMeta(filename)

Trans trans = new Trans(transMeta)

trans.prepareExecution(null)

trans.startThreads()

trans.waitUntilFinished()

if (trans.getErrors()!=0) {

System.out.println("Error")

}

} catch (KettleXMLException e) {

//尺蠢 TODO Auto-generated catch block

e.printStackTrace()

} catch (KettleException e) {

//扒渣 TODO Auto-generated catch block

e.printStackTrace()

}

}

public static void main(String[] args) {

String jobname="D:/syslog_All/spoon/job_csv_bsf.kjb"

runJob(jobname)

// String filename="D:/syslog_All/spoon/csv_fm.ktr"

// runTrans(filename)

}

}

如下为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()

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存