先做一个jar工程,这个工程是jdk1.7的,使用maven
说明:这个东西需要使用Spoon.bat设计转换,然后使用java调用Spoon.bat设置的demo.ktr文件
目录结构如下:
首先需要下载kettle6,连接地址https://sourceforge.net/projects/pentaho/files/Data%20Integration/6.1/
解压后目录结构:
进入lib目录拷贝三个jar包,kettle-core-6.1.0.1-196.jar,kettle-engine-6.1.0.1-196.jar,metastore-6.1.0.1-196.jar
放入新建工程src/libs目录下,将这个三个依赖安装的maven本地创库
mvn install:install-file -Dfile=./kettle-core-6.1.0.1-196.jar -DgroupId=pentaho-kettle -DartifactId=kettle-core -Dversion=6.1.0.1-196 -Dpackaging=jar
mvn install:install-file -Dfile=./kettle-engine-6.1.0.1-196.jar -DgroupId=pentaho-kettle -DartifactId=kettle-engine -Dversion=6.1.0.1-196 -Dpackaging=jar
mvn install:install-file -Dfile=./metastore-6.1.0.1-196.jar -DgroupId=pentaho-kettle -DartifactId=metastore -Dversion=6.1.0.1-196 -Dpackaging=jar
在然后在工程中配置如下:
<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>com.szhis.frsoft</groupId>
<artifactId>hsp-plugins-mr-guangdong</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!-- Generic properties -->
<java.version>1.7</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- kettle -->
<kettle.version>6.1.0.1-196</kettle.version>
</properties>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-vfs2</artifactId>
<version>2.0</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>17.0</version>
</dependency>
<!-- kettle -->
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-core</artifactId>
<version>${kettle.version}</version>
</dependency>
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-engine</artifactId>
<version>${kettle.version}</version>
</dependency>
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>metastore</artifactId>
<version>${kettle.version}</version>
</dependency>
<!-- jdbc驱动 -->
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 打包,构建 -->
<build>
<!-- 插件 -->
<plugins>
<!-- jar包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 打包跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
java运行测试代码
package com.szhis.frsoft.demo1;
import org.junit.Test;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
public class TransferTest {
@Test
public void test_helloWorld() {
String project_path = getFilePath();
// String filename = "demo1/demo1.ktr";
String filename = "demo1/demo-ssl.ktr";
filename = project_path + filename;
// 调用转换
try {
transfer(filename);
} catch (KettleException e) {
e.printStackTrace();
}
}
public void transfer(String filename) throws KettleException {
KettleEnvironment.init();
TransMeta tm = new TransMeta(filename);
Trans trans = new Trans(tm);
// 空参调用
trans.execute(null);
trans.waitUntilFinished();
}
// 获得文件绝对地址(项目绝对地址+我的文件相对项目的地址)
private String getFilePath() {
String absolute = this.getClass().getClassLoader().getResource("").getPath();
if (absolute.startsWith("/")) {
absolute = absolute.substring(1);
}
return absolute;
}
@Test
public void test_path() {
String filePath = getFilePath();
System.out.println("我的控制台输出");
System.out.println(filePath);
}
}
在看一下项目结构
kettle设置文件界面
数据库数据
注意这个两张表的结构是不一样的,本案例仅提供参考