java集成kettle:执行作业文件

执行目标:

将D:/from.txt中的内容复制到D:/to.txt中

前置条件:在D:下有3个文件

 项目结构:

 pom.xml文件

<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>www.test.com</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>pentaho-kettle</groupId>
            <artifactId>kettle-core</artifactId>
            <version>7.1.0.0-12</version>
        </dependency>
        <dependency>
            <groupId>pentaho-kettle</groupId>
            <artifactId>kettle-dbdialog</artifactId>
            <version>7.1.0.0-12</version>
        </dependency>

        <dependency>
            <groupId>pentaho-kettle</groupId>
            <artifactId>kettle-engine</artifactId>
            <version>7.1.0.0-12</version>
        </dependency>

        <dependency>
            <groupId>pentaho</groupId>
            <artifactId>metastore</artifactId>
            <version>7.1.0.0-12</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-vfs2 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-vfs2</artifactId>
            <version>2.2</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>pentaho-releases</id>
            <name>Kettle</name>
            <url>https://nexus.pentaho.org/content/groups/omni/</url>
        </repository>
        <!-- 阿里云maven仓库 -->
        <repository>
            <id>public</id>
            <name>aliyun nexus</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>
</project>

KettleTest.java文件

package test;

import java.io.File;

import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.job.Job;
import org.pentaho.di.job.JobMeta;

/**
 * @author cgm
 * @date 2019年7月8日
 */
public class KettleTest {
    public static void main(String[] args) throws Exception {
        KettleEnvironment.init();
        String path = "D:" + File.separator + "job1.kjb";
        JobMeta jobMeta = new JobMeta(path, null);
        Job job = new Job(null, jobMeta);
        job.start();
        job.waitUntilFinished();
        if (job.getErrors() > 0) {
            System.out.println("执行失败");
            throw new KettleException("There are errors during job exception!(执行job发生异常)");
        } else {
            System.out.println("执行成功");
        }
    }
}
 

执行后

发现D:/to.txt中的文件内容等于D:/from.txt文件中的内容

完毕!

附件:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用Spring Boot来集成Kettle,以下是一些步骤: 1. 首先,将Kettle的依赖添加到你的Spring Boot项目中。可以在项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.pentaho</groupId> <artifactId>kettle-core</artifactId> <version>8.3.0.0-371</version> </dependency> ``` 2. 创建一个Kettle的配置类,用于初始化Kettle环境。在这个类中,你可以设置Kettle的一些配置信息,如数据库连接等。示例代码如下: ```java @Configuration public class KettleConfig { @PostConstruct public void init() throws KettleException { KettleEnvironment.init(); } // 其他配置信息 } ``` 3. 创建一个Kettle的服务类,用于执行Kettle的转换或作业。在这个类中,你可以使用Kettle提供的API来加载、运行和监控转换或作业。示例代码如下: ```java @Service public class KettleService { public void runTransformation(String transformationPath) throws KettleException { // 加载转换 TransMeta transMeta = new TransMeta(transformationPath); // 创建转换 Trans trans = new Trans(transMeta); // 执行转换 trans.execute(null); // 等待转换完成 trans.waitUntilFinished(); // 获取转换执行结果 Result result = trans.getResult(); // 处理转换执行结果 // ... } public void runJob(String jobPath) throws KettleException { // 加载作业 JobMeta jobMeta = new JobMeta(jobPath, null); // 创建作业 Job job = new Job(null, jobMeta); // 执行作业 job.start(); // 等待作业完成 job.waitUntilFinished(); // 获取作业执行结果 Result result = job.getResult(); // 处理作业执行结果 // ... } } ``` 4. 在你的业务代码中,可以通过注入KettleService来使用Kettle的功能。例如: ```java @RestController public class MyController { @Autowired private KettleService kettleService; @GetMapping("/runTransformation") public String runTransformation() throws KettleException { String transformationPath = "path/to/your/transformation.ktr"; kettleService.runTransformation(transformationPath); return "Transformation executed successfully."; } @GetMapping("/runJob") public String runJob() throws KettleException { String jobPath = "path/to/your/job.kjb"; kettleService.runJob(jobPath); return "Job executed successfully."; } } ``` 以上是一个简单的示例,你可以根据你的具体需求进行相应的配置和调整。希望对你有帮助!如有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cgm625637391

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值