java以api的方式集成调用kettle

一、kettle集成调用的多种方式。

kettle集成调用的方式有多种,比如:

1、脚本的方式,windows下以bat脚本调用,Linux下以sh脚本调用。

2、http请求carte服务的方式进行集成调用。java端可以采用httpclient api去调用carte。

3、以java工程引入kettle依赖,采用api集成的方式调用。

二、为什么要以api的方式集成kettle

因为第一点提到的1、2两点,都需要在服务节点端安装kettle,且脚本的方式调用要维护多套系统执行脚本。而以carte服务http请求调用的方式,又会涉及集群部署的问题。这两点都增加了运维的成本。本人从事银行金融方向的系统产品研发,接触过多家银行。银行业比较抵触安装过多的组件。集成方案越简单,安全稳定系数越高越好。所以个人比较推崇以api的方式集成kettle。此种方式只需要将利用spoon可视化界面编写好的流程脚本放在指定的磁盘目录下,用api的方式调用执行即可。

三、集成

因为maven管理比较方便,所以本人将kettle所有的依赖包本地编译安装至maven私有仓库,然后在pom中引入相关依赖的方式集成安装。以下是依赖和集成安装方式:

3.1、下载kettle相关依赖:

百度网盘地址:(过期的话,请评论区留言获取新地址,谢谢)

链接:https://pan.baidu.com/s/1gpiOkT1QnKKQCLmi7GRjrA 
提取码:mcm3

3.2、编译安装至本地maven库

mvn install:install-file -Dfile=E:\job\lib\kettle-core-8.3.0.0-371.jar -DgroupId=org.pentaho -DartifactId=kettle-core -Dversion=8.3 -Dpackaging=jar
mvn install:install-file -Dfile=E:\job\lib\kettle-engine-8.3.0.0-371.jar -DgroupId=org.pentaho -DartifactId=kettle-engine -Dversion=8.3 -Dpackaging=jar
mvn install:install-file -Dfile=E:\job\lib\metastore-8.3.0.0-371.jar -DgroupId=org.pentaho -DartifactId=metastore -Dversion=8.3 -Dpackaging=jar
mvn install:install-file -Dfile=E:\job\lib\pentaho-vfs-browser-8.3.0.0-371.jar -DgroupId=org.pentaho -DartifactId=pentaho-vfs-browser -Dversion=8.3 -Dpackaging=jar
mvn install:install-file -Dfile=E:\job\lib\pdi-dataservice-client-plugin-8.3.0.0-371.jar -DgroupId=org.pentaho -DartifactId=pdi-dataservice-client-plugin -Dversion=8.3 -Dpackaging=jar
mvn install:install-file -Dfile=E:\job\lib\pdi-dataservice-client-plugin-api-8.3.0.0-371.jar -DgroupId=org.pentaho -DartifactId=pdi-dataservice-client-plugin-api -Dversion=8.3 -Dpackaging=jar
mvn install:install-file -Dfile=E:\job\lib\pentaho-mongo-utils-8.3.0.0-371.jar -DgroupId=org.pentaho -DartifactId=pentaho-mongo-utils -Dversion=8.3 -Dpackaging=jar
mvn install:install-file -Dfile=E:\job\lib\pentaho-mongodb-plugin-8.3.0.0-371.jar -DgroupId=org.pentaho -DartifactId=pentaho-mongodb-plugin -Dversion=8.3 -Dpackaging=jar
mvn install:install-file -Dfile=E:\job\lib\scannotation-1.0.2.jar -DgroupId=org.pentaho -DartifactId=scannotation -Dversion=1.0.2 -Dpackaging=jar
mvn install:install-file -Dfile=E:\job\lib\json-8.3.0.0-371.jar -DgroupId=org.pentaho -DartifactId=json -Dversion=8.3 -Dpackaging=jar
mvn install:install-file -Dfile=E:\job\lib\jsonpath-1.0.jar -DgroupId=org.pentaho -DartifactId=jsonpath -Dversion=1.0 -Dpackaging=jar
mvn install:install-file -Dfile=E:\job\lib\json-simple-1.1.jar -DgroupId=org.pentaho -DartifactId=json-simple -Dversion=1.1 -Dpackaging=jar
mvn install:install-file -Dfile=E:\job\lib\simple-jndi-1.0.2.jar -DgroupId=org.pentaho -DartifactId=simple-jndi -Dversion=1.0.2 -Dpackaging=jar
mvn install:install-file -Dfile=E:\job\lib\javax.mail-1.6.1.jar -DgroupId=javax.mail -DartifactId=mail -Dversion=1.6.1 -Dpackaging=jar

3.3、maven引入:

kettle引入:

<properties>
    <kettle.version>8.3</kettle.version>
    <skipTests>true</skipTests>
</properties>
<dependency>
    <groupId>org.pentaho</groupId>
    <artifactId>kettle-core</artifactId>
    <version>${kettle.version}</version>
</dependency>
<dependency>
    <groupId>org.pentaho</groupId>
    <artifactId>kettle-engine</artifactId>
    <version>${kettle.version}</version>
</dependency>
<dependency>
    <groupId>org.pentaho</groupId>
    <artifactId>metastore</artifactId>
    <version>${kettle.version}</version>
</dependency>
<dependency>
    <groupId>org.pentaho</groupId>
    <artifactId>pentaho-vfs-browser</artifactId>
    <version>${kettle.version}</version>
</dependency>
<dependency>
    <groupId>org.pentaho</groupId>
    <artifactId>pdi-dataservice-client-plugin</artifactId>
    <version>${kettle.version}</version>
</dependency>
<dependency>
    <groupId>org.pentaho</groupId>
    <artifactId>pdi-dataservice-client-plugin-api</artifactId>
    <version>${kettle.version}</version>
</dependency>
<dependency>
    <groupId>org.pentaho</groupId>
    <artifactId>pentaho-mongo-utils</artifactId>
    <version>${kettle.version}</version>
</dependency>
<dependency>
    <groupId>org.pentaho</groupId>
    <artifactId>pentaho-mongodb-plugin</artifactId>
    <version>${kettle.version}</version>
</dependency>
<dependency>
    <groupId>org.pentaho</groupId>
    <artifactId>scannotation</artifactId>
    <version>1.0.2</version>
</dependency>
<dependency>
    <groupId>org.pentaho</groupId>
    <artifactId>json</artifactId>
    <version>${kettle.version}</version>
</dependency>
<dependency>
    <groupId>org.pentaho</groupId>
    <artifactId>jsonpath</artifactId>
    <version>1.0</version>
</dependency>
<dependency>
    <groupId>org.pentaho</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1</version>
</dependency>
<dependency>
    <groupId>org.pentaho</groupId>
    <artifactId>simple-jndi</artifactId>
    <version>1.0.2</version>
</dependency>
 <dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.6.1</version>
</dependency>

间接依赖引入:

<!-- 其他需要的基础包 -->
<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.20.0-GA</version>
</dependency>
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>17.0</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.7</version>
</dependency>
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.10</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-vfs2</artifactId>
    <version>2.2</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.2</version>
</dependency>
<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
</dependency>
<dependency>
    <groupId>org.codehaus.janino</groupId>
    <artifactId>commons-compiler</artifactId>
    <version>3.0.8</version>
</dependency>
<dependency>
    <groupId>org.codehaus.janino</groupId>
    <artifactId>janino</artifactId>
    <version>3.0.8</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.14</version>
</dependency>
<dependency>
    <groupId>io.reactivex.rxjava2</groupId>
    <artifactId>rxjava</artifactId>
    <version>2.2.3</version>
</dependency>
<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.54</version>
</dependency>
<dependency>
    <groupId>org.mozilla.javascript</groupId>
    <artifactId>com.springsource.org.mozilla.javascript</artifactId>
    <version>1.7.0.R2</version>
</dependency>
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1</version>
</dependency>
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.6.1</version>
</dependency>
<dependency>
    <groupId>org.jetbrains</groupId>
    <artifactId>annotations</artifactId>
    <version>13.0</version>
    <scope>compile</scope>
</dependency>

四、执行集成调用测试:

源码地址:(过期的话,请评论区留言获取新地址,谢谢)

链接:https://pan.baidu.com/s/1L_BoJ55SJtzw0s6NqINTMQ 
提取码:zizj

五、结果截图:

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 56
    评论
Kettle 是一款开源的 ETL 工具,提供了丰富的 API,可以通过 Java 调用 Kettle API 实现数据同步。下面是一个简单的示例: 1. 导入 Kettle 依赖包 Kettle 的依赖包可以从官网下载,也可以通过 Maven 引入。在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.pentaho</groupId> <artifactId>kettle-core</artifactId> <version>9.1.0.0-324</version> </dependency> ``` 2. 创建 Kettle 连接 使用 Kettle API 前需要创建一个 Kettle 连接,可以通过以下代码创建: ```java KettleEnvironment.init(); DatabaseMeta databaseMeta = new DatabaseMeta("mysql", "mysql", "Native (JDBC)", "localhost", "3306", "test", "root", "password"); Database database = new Database(null, databaseMeta); database.connect(); ``` 其中,`DatabaseMeta` 是数据库连接的元数据,包括数据库类型、主机名、端口号、数据库名、用户名和密码等信息;`Database` 是数据库连接对象,通过 `connect()` 方法连接数据库。 3. 创建转换并执行 Kettle 中的数据同步是通过转换(Transformation)实现的,转换包含了数据源、目标、转换规则等信息。可以通过以下代码创建转换并执行: ```java TransMeta transMeta = new TransMeta("path/to/transformation.ktr"); Trans trans = new Trans(transMeta); trans.execute(null); trans.waitUntilFinished(); if (trans.getErrors() > 0) { throw new RuntimeException("Transformation failed with errors!"); } ``` 其中,`TransMeta` 是转换元数据,包含了转换的各种信息,如输入输出、转换步骤等;`Trans` 是转换对象,通过 `execute()` 方法执行转换,`waitUntilFinished()` 方法等待转换完成,`getErrors()` 方法获取转换过程中的错误数量。 以上是基本的 Kettle API 调用流程,具体的实现还需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 56
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值