java集成kettle:执行作业文件

本文介绍了一种使用Pentaho Data Integration (Kettle)工具从一个文件复制内容到另一个文件的方法。通过Java代码调用Kettle环境初始化,并运行预先配置的作业,成功实现了D盘上from.txt文件的内容复制到to.txt文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

执行目标:

将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文件中的内容

完毕!

附件:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

cgm625637391

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

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

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

打赏作者

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

抵扣说明:

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

余额充值