这几天小白得到了一个任务,需要写一个简单的程序,执行当地的ktr文件,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("执行完成!"); } else { System.out.println("完成执行!失败的转为:" errorFile); } } } } }
依赖
要想运行这个代码,需要增加很多依赖性,因为有些依赖性缺失是可以启动的,只有在运行时才会报告"class not found",接下来我贴出我的pom文件支持三个数据库Oracle,sqlserver,达梦数据库。
注意:由于kettle包不能通过maven所以你需要先下载,然后通过maven的<scope><systemPath>设置这两个标签。
<dependencies> <dependency> <groupId&t;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文件放入其中即可。