发布于: 2020 年 05 月 02 日
kettle(Pentaho Data Integration) 使用"最佳"实践 - 图1

kettle 是一款基于JAVA的开源ETL工具,现在它的名字应该叫做Pentaho Data Integration(PDI)了, 非常优秀的一款工具,功能非常多,今天我们主要演示其Java API案例

案例

通过PDI工具,完成一个Job,主要目标是将表中的数据导出到dat文件中,导出的文件名字以user-开头,内容使用|作为分割符,后面的格式是YYYYMMDD后缀为dat. 例:user-20200502.dat

下载PDI

https://sourceforge.net/projects/pentaho/files/latest/download?aliId=137249511

启动PDI

linux环境下通过命令sh spoon.sh命令来启动,打开后如果对英文不熟悉,可以在工具菜单页修改默认语言,重启后生效

开始

打开之后,映入眼帘的是Job(作业)Transformation(转换)这里我们新建一个Job,作业中包含一个transformation,Job的作用是为了控制过程,transformation在这里启到的作用是将表中的数据写入到dat文件中

Job如下:
kettle(Pentaho Data Integration) 使用"最佳"实践 - 图2

transformation如下:
kettle(Pentaho Data Integration) 使用"最佳"实践 - 图3

整个过程非常简单,托拉拽就完成了,特别说明几处要点。

双击表输入弹出如下窗口,没有数据库连接的情况下,需要点击数据库连接右侧的新建按钮,注意选择Generic database,如果提示找不到驱动,那么下载一个放到PDI的lib目录下重启即可

kettle(Pentaho Data Integration) 使用"最佳"实践 - 图4

kettle(Pentaho Data Integration) 使用"最佳"实践 - 图5

双击文本文件输出文件页签中文件名称栏位可以制定文件路径,文件名可以是部分文件名,如下图的user-,这里扩展名栏位我们输入dat,指定日期格式栏位打钩,日期格式栏位是yyyyMMdd,在结果中添加文件名栏位打钩,显示文件名点击后可以预览最终生成的文件名称。

内容页签中分割符栏位修改为|

字段页签中一定要点击一下获取字段,否则字段页签全部为空,Job跑起来的时候也会报错。

kettle(Pentaho Data Integration) 使用"最佳"实践 - 图6

kettle(Pentaho Data Integration) 使用"最佳"实践 - 图7

kettle(Pentaho Data Integration) 使用"最佳"实践 - 图8

Java工程结构

kettle(Pentaho Data Integration) 使用"最佳"实践 - 图9

Maven依赖

本次案例主要依赖如下jar包,其实也是kettle主要的依赖jar

  1. <dependency>
  2. <groupId>org.pentaho</groupId>
  3. <artifactId>kettle-core</artifactId>
  4. <version>9.0.0.0-423</version>
  5. <scope>system</scope>
  6. <systemPath>${project.basedir}/src/main/resources/lib/kettle-core-9.0.0.0-423.jar</systemPath>
  7. </dependency>
  8. <dependency>
  9. <groupId>org.pentaho</groupId>
  10. <artifactId>kettle-engine</artifactId>
  11. <version>9.0.0.0-423</version>
  12. <scope>system</scope>
  13. <systemPath>${project.basedir}/src/main/resources/lib/kettle-engine-9.0.0.0-423.jar</systemPath>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.pentaho</groupId>
  17. <artifactId>metastore</artifactId>
  18. <version>9.0.0.0-423</version>
  19. <scope>system</scope>
  20. <systemPath>${project.basedir}/src/main/resources/lib/metastore-9.0.0.0-423.jar</systemPath>
  21. </dependency>
  22. <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-vfs2 -->
  23. <dependency>
  24. <groupId>org.apache.commons</groupId>
  25. <artifactId>commons-vfs2</artifactId>
  26. <version>2.1</version>
  27. </dependency>
  28. <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
  29. <dependency>
  30. <groupId>com.google.guava</groupId>
  31. <artifactId>guava</artifactId>
  32. <version>29.0-jre</version>
  33. </dependency>
  34. <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
  35. <dependency>
  36. <groupId>commons-io</groupId>
  37. <artifactId>commons-io</artifactId>
  38. <version>2.6</version>
  39. </dependency>
  40. <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
  41. <dependency>
  42. <groupId>commons-lang</groupId>
  43. <artifactId>commons-lang</artifactId>
  44. <version>2.6</version>
  45. </dependency>
  46. <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
  47. <dependency>
  48. <groupId>commons-codec</groupId>
  49. <artifactId>commons-codec</artifactId>
  50. <version>1.14</version>
  51. </dependency>
  52. <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
  53. <dependency>
  54. <groupId>org.projectlombok</groupId>
  55. <artifactId>lombok</artifactId>
  56. <version>1.18.12</version>
  57. <scope>provided</scope>
  58. </dependency>
  59. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  60. <dependency>
  61. <groupId>mysql</groupId>
  62. <artifactId>mysql-connector-java</artifactId>
  63. <version>8.0.20</version>
  64. </dependency>


运行Job

  1. package com.example.fg.kettle;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.pentaho.di.core.KettleEnvironment;
  4. import org.pentaho.di.core.Result;
  5. import org.pentaho.di.core.exception.KettleException;
  6. import org.pentaho.di.core.util.EnvUtil;
  7. import org.pentaho.di.job.Job;
  8. import org.pentaho.di.job.JobMeta;
  9. import org.pentaho.di.trans.Trans;
  10. import org.pentaho.di.trans.TransMeta;
  11. import java.io.File;
  12. import java.net.URISyntaxException;
  13. import java.net.URL;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. /*
  17. @author cattle - 稻草鸟人
  18. @date 2020/4/30 下午5:09
  19. /
  20. @Slf4j
  21. public class KettleTools {
  22. public static void runJob(String jobName, Map<String, String> variable) {
  23. try {
  24. KettleEnvironment.init();
  25. JobMeta jobMeta = new JobMeta(jobName, null);
  26. Job job = new Job(null, jobMeta);
  27. variable.forEach((key, value) -> {
  28. job.setVariable(key, value);
  29. });
  30. job.start();
  31. job.waitUntilFinished();
  32. if (job.getErrors() > 0) {
  33. throw new Exception("There were errors during job execution");
  34. }
  35. } catch (Exception e) {
  36. log.error("runJob exception,jobName={}, exception={}", jobName, e);
  37. }
  38. }
  39. public static void main(String[] args) throws URISyntaxException {
  40. URL url = KettleTools.class.getResource("/kettle/kettle-job-demo.kjb");
  41. runJob(new File(url.toURI()).getAbsolutePath(), new HashMap<>());
  42. }
  43. }

执行完成后,就可以在桌面上看到一个user-20200502.dat的文件,内容如下:

kettle(Pentaho Data Integration) 使用"最佳"实践 - 图10

参考文档

[^PDI document]: Latest Pentaho Data Integration (aka Kettle) Documentation+Documentation)](https://wiki.pentaho.com/display/EAI/Latest+Pentaho+Data+Integration+(aka+Kettle)+Documentation))

源码地址

https://github.com/cattles/fucking-great-kettle.git

kettle(Pentaho Data Integration) 使用"最佳"实践 - 图11