锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

KETTLE:通过代码运行本地ktr文件

时间:2023-08-20 12:37:00 ktr自动复位电阻尺ktr微型自恢复直线位移传感器

这几天小白得到了一个任务,需要写一个简单的程序,执行当地的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 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的设置这两个标签

                          
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            cn.hutool
            hutool-crypto
            5.3.2
            compile
        
        
            commons-configuration
            commons-configuration
            1.10
            compile
        
        
            commons-configuration
            commons-configuration
            1.10
            compile
        
        
            org.pentaho.di
            kettle-engine
            8.2.0.0
            system
            ${project.basedir}/src/lib/xxxxxxxxxx.jar
        
        
            org.pentaho.di
            kettle-core
            8.2.0.0
            system
            ${project.basedir}/src/lib/xxxxx.jar
        
        
            org.pentaho.metastore
            metastore
            8.2.0.0
            system
            ${project.basedir}/src/lib/xxxxx.jar
        
        
            net.sourceforge.jtds
            jtds
            1.2.5
            system
            ${project.basedir}/src/lib/xxxxx.jar
        
        
            org.apache.commons
            commons-vfs2
            2.1
        
        
            org.slf4j
            slf4j-log4j12
            1.7.25
        
        
            com.google.guava
            guava
            25.1-jre
        
        
            commons-io
            commons-io
            2.4
        
        
            commons-codec
            commons-codec
            1.10
        
        
            com.oracle
            ojdbc6// oracle数据库驱动
            xxxxxx
        
        
            com.microsoft.sqlserver
            mssql-jdbc// sqlserver数据库驱动
            xxxxxxx
        
        
            com.dameng
            Dm7JdbcDriver18//  达梦数据库驱动
            xxxxxxx
        
        
            commons-beanutils
            commons-beanutils
            1.9.3
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    ZIP
                    true
                    true
                
            
        

    

ktr文件

最后打包,在jar包同级别命名一个ktrs文件夹,将需要执行的ktr文件放入其中即可。

锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章