执行目标:
将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文件中的内容
完毕!
附件: