这几天小白得到了一个任务,需要写一个简单的程序,执行本地的ktr文件,ktr文件中会更新数据库数据,且数据库连接信息需从执行这段程序的机器的资源库中获取,再次记录下这段程序。
目录
工程
首先,新建一个springboot工程,方便将依赖包打包进jar文件。
代码
然后main函数代码如下代码所示,其中有的变量我重新命名了下。
package com.xxxx.xxxxa;
import xxxxxx;
@SpringBootApplication
public class xxxxApplication {
public static void main(String[] args) throws Exception{
KettleEnvironment.init();//初始化环境
RepositoriesMeta repositoriesMeta = new RepositoriesMeta();
if (repositoriesMeta.readData()) {
KettleDatabaseRepositoryMeta xxxRepo = (KettleDatabaseRepositoryMeta) repositoriesMeta.searchRepository("xxx-repo");
String databaseMetaName = (null == xxxRepo ? "xxx-repo-connection" : xxxRepo.getConnection().getName());
DatabaseMeta xxxRepoConn = repositoriesMeta.searchDatabase(databaseMetaName);
System.out.println("连接名称:" + xxxRepoConn);
if (null == xxxRepoConn) {
throw new Exception("xxx资源库配置有误,请重新配置后再次运行!");
} else {
// 数据库信息确认
Scanner sc = new Scanner(System.in);
System.out.println("请确认需要执行的数据库信息,如正确,输入Y后回车执行;如错误,请检查xxx资源库配置。");
System.out.println("数据库类型:" + xxxRepoConn.getPluginId()); //数据库类型
System.out.println("连接地址:" + xxxRepoConn.getHostname()); //连接地址
System.out.println("数据库名称" + xxxRepoConn.getDatabaseName()); //数据库名称
System.out.println("端口" + xxxRepoConn.getDatabasePortNumberString()); //端口
System.out.println("用户" + xxxRepoConn.getUsername()); //用户
String indexStr = sc.nextLine();
if(!"Y".equalsIgnoreCase(indexStr)){
throw new Exception("取消执行!");
}
String errorFile = null;
String dir = new ApplicationHome().toString() + "/ktrs/" ;
System.out.println(new ApplicationHome());
System.out.println(dir);
File file = new File(dir);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile() && tempList[i].getName().endsWith(".ktr")) {
System.out.println("文件路径:" + dir + tempList[i].getName());
TransMeta transMeta = new TransMeta(dir + tempList[i].getName());//new tran的源数据对象
// 设置数据库信息
List<DatabaseMeta> dmlist=transMeta.getDatabases();
for(DatabaseMeta dm : dmlist){
dm.setDatabaseType(xxxRepoConn.getPluginId()); //数据库类型
dm.setHostname(xxxRepoConn.getHostname()); //连接地址
dm.setDBName(xxxRepoConn.getDatabaseName()); //数据库名称
dm.setDBPort(xxxRepoConn.getDatabasePortNumberString()); //端口
dm.setUsername(xxxRepoConn.getUsername()); //用户
dm.setPassword(xxxRepoConn.getPassword()); //密码
}
Trans trans = new Trans(transMeta);//创建tran对象
trans.prepareExecution(null);//异常处理
trans.startThreads();//开始执行
trans.waitUntilFinished();//等待执行完毕
if(trans.getErrors()!=0){
errorFile += tempList[i].getName() + ";";
System.out.println("Error encountered!");
} else {
tempList[i].delete();
}
}
}
if (null == errorFile) {
System.out.println("执行成功!");
} else {
System.out.println("执行完成!其中失败的转换为:" + errorFile);
}
}
}
}
}
依赖
想要能运行起来这段代码,还需要增加很多依赖,因为有些依赖缺失是可以启动的,当你运行的时候才会报"class not found",我接下来贴出来我的pom文件,支持三个数据库,分别为Oracle,sqlserver,达梦数据库。
注意:由于kettle的包无法通过maven下载,所以需要自己先下载,然后通过maven的<scope> <systemPath>这两个标签进行设置。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-crypto</artifactId>
<version>5.3.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.10</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.10</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.pentaho.di</groupId>
<artifactId>kettle-engine</artifactId>
<version>8.2.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/lib/xxxxxxxxxx.jar</systemPath>
</dependency>
<dependency>
<groupId>org.pentaho.di</groupId>
<artifactId>kettle-core</artifactId>
<version>8.2.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/lib/xxxxx.jar</systemPath>
</dependency>
<dependency>
<groupId>org.pentaho.metastore</groupId>
<artifactId>metastore</artifactId>
<version>8.2.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/lib/xxxxx.jar</systemPath>
</dependency>
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.2.5</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/lib/xxxxx.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-vfs2</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.1-jre</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>// oracle数据库驱动
<version>xxxxxx</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>// sqlserver数据库驱动
<version>xxxxxxx</version>
</dependency>
<dependency>
<groupId>com.dameng</groupId>
<artifactId>Dm7JdbcDriver18</artifactId>// 达梦数据库驱动
<version>xxxxxxx</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<fork>true</fork>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
ktr文件
最后打包,在jar包同级别命名一个ktrs文件夹,将需要执行的ktr文件放入其中即可。