Springboot整合kettle项目案例

3 篇文章 0 订阅
1 篇文章 0 订阅

一、前言

地址:Kettle中文网

Kettle是一款国外开源的ETL工具,纯java编写,可以在Window、Linux、Unix上运行,绿色无需安装,数据抽取高效稳定。
Kettle 中文名称叫水壶,该项目的主程序员MATT 希望把各种数据放到一个壶里,然后以一种指定的格式流出。
Kettle这个ETL工具集,它允许你管理来自不同数据库的数据,通过提供一个图形化的用户环境来描述你想做什么,而不是你想怎么做。
Kettle中有两种脚本文件,transformation和job,transformation完成针对数据的基础转换,job则完成整个工作流的控制。
Kettle(现在已经更名为PDI,Pentaho Data Integration-Pentaho数据集成)。

二、环境

1.JDK1.8
2.Kettle8.3

三、项目结构

在这里插入图片描述

3.1 修改pom文件

说明:kettle用到的部分包依赖(可从kettle客户端lib包拷贝,可自行打到自己maven仓库,因是测试案例本人是本地引用)

    <!-- Spring boot 父引用-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <!-- Spring boot 核心web 整合了spring +springMVC + mybatis-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.46</version>
        </dependency>

        <dependency>
            <groupId>com.google.common</groupId>
            <artifactId>guava</artifactId>
            <version>17.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/guava-17.0.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>

        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/commons-codec-1.10.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.pentaho.di</groupId>
            <artifactId>kettle-core</artifactId>
            <version>8.3.0.0-371</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/kettle-core-8.3.0.0-371.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>org.pentaho.di</groupId>
            <artifactId>kettle-engine</artifactId>
            <version>8.3.0.0-371</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/kettle-engine-8.3.0.0-371.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.pentaho.metastore</groupId>
            <artifactId>metastore</artifactId>
            <version>8.3.0.0-371</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/metastore-8.3.0.0-371.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-vfs2</artifactId>
            <version>2.2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/commons-vfs2-2.2.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>net.sourceforge</groupId>
            <artifactId>jtds</artifactId>
            <version>1.3.3</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/jcifs-1.3.3.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/commons-logging-1.1.3.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.2</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/commons-io-2.2.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>oracle</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>8.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/ojdbc8.jar</systemPath>
        </dependency>

    </dependencies>

3.2 Kettle工具类

public class KettleUtils {

    /**
     * 执行ktr文件
     * @param filename
     * @param params
     * @return
     */
    public static   void runKtr(String filename, Map<String, String> params, String dirPath) {
        try {
            KettleEnvironment.init();
            TransMeta tm = new TransMeta(dirPath + File.separator + filename);
            Trans trans = new Trans(tm);
            if (params != null) {
                Iterator<Map.Entry<String, String>> entries = params.entrySet().iterator();
                while (entries.hasNext()) {
                    Map.Entry<String, String> entry = entries.next();
                    trans.setParameterValue(entry.getKey(),entry.getValue());
                }
            }
            trans.execute(null);
            trans.waitUntilFinished();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 执行kjb文件
     * @param filename
     * @param params
     * @return
     */
    public static void runKjb(String filename, Map<String, String> params, String dirPath){
        try {
            KettleEnvironment.init();
            JobMeta jm = new JobMeta(dirPath + File.separator + filename, null);
            Job job = new Job(null, jm);
            if (params != null) {
                Iterator<Map.Entry<String, String>> entries = params.entrySet().iterator();
                while (entries.hasNext()) {
                    Map.Entry<String, String> entry = entries.next();
                    job.setVariable(entry.getKey(), entry.getValue());
                }
            }
            job.start();
            job.waitUntilFinished();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        ClassPathResource classPathResource = new ClassPathResource("kettle");
        System.out.println("classPathResource:" + classPathResource.getFile().getPath());
        runKjb("test_kettle.kjb", null, classPathResource.getFile().getPath());
    }

四、Kettle脚本

说明:转换ktr脚本和作业kjb脚本由可视化kettle先编写好

五、测试

说明:工具类简单测试

在这里插入图片描述

Spring Boot集成Kettle可以实现数据抽取、转换和加载(ETL)的功能。具体步骤如下: 1. 在pom.xml文件中添加Kettle的依赖: ``` <dependency> <groupId>org.pentaho</groupId> <artifactId>kettle-core</artifactId> <version>8.3..-371</version> </dependency> ``` 2. 创建Kettle的配置文件,例如kettle.properties,配置Kettle的相关参数,如数据库连接信息、文件路径等。 3. 在Spring Boot的配置文件中,添加Kettle的配置信息,如下所示: ``` @Configuration public class KettleConfig { @Value("${kettle.home}") private String kettleHome; @Value("${kettle.properties.file}") private String kettlePropertiesFile; @Bean public KettleEnvironment kettleEnvironment() throws KettleException { KettleEnvironment.init(false); System.setProperty("KETTLE_HOME", kettleHome); System.setProperty("KETTLE_PROPERTIES", kettlePropertiesFile); return KettleEnvironment.getInstance(); } @Bean public KettleDatabaseRepository kettleDatabaseRepository() throws KettleException { KettleDatabaseRepositoryMeta kettleDatabaseRepositoryMeta = new KettleDatabaseRepositoryMeta(); kettleDatabaseRepositoryMeta.setName("KettleDatabaseRepository"); kettleDatabaseRepositoryMeta.setConnection(new DatabaseMeta("Kettle", "MYSQL", "Native", "localhost", "kettle", "3306", "root", "root")); KettleDatabaseRepository kettleDatabaseRepository = new KettleDatabaseRepository(); kettleDatabaseRepository.init(kettleDatabaseRepositoryMeta); kettleDatabaseRepository.connect("admin", "admin"); return kettleDatabaseRepository; } } ``` 4. 编写Kettle的作业和转换,例如job.kjb和trans.ktr,放置在resources目录下。 5. 在Spring Boot中调用Kettle的作业和转换,如下所示: ``` @Service public class KettleService { @Autowired private KettleEnvironment kettleEnvironment; @Autowired private KettleDatabaseRepository kettleDatabaseRepository; public void runJob(String jobName) throws KettleException { JobMeta jobMeta = new JobMeta(kettleEnvironment, jobName, null); Job job = new Job(kettleDatabaseRepository, jobMeta); job.start(); job.waitUntilFinished(); if (job.getErrors() > ) { throw new KettleException("Job " + jobName + " failed with " + job.getErrors() + " errors."); } } public void runTrans(String transName) throws KettleException { TransMeta transMeta = new TransMeta(kettleEnvironment, transName); Trans trans = new Trans(transMeta); trans.execute(null); trans.waitUntilFinished(); if (trans.getErrors() > ) { throw new KettleException("Trans " + transName + " failed with " + trans.getErrors() + " errors."); } } } ``` 6. 在Controller中调用KettleService的方法,如下所示: ``` @RestController public class KettleController { @Autowired private KettleService kettleService; @GetMapping("/runJob") public String runJob(@RequestParam String jobName) throws KettleException { kettleService.runJob(jobName); return "Job " + jobName + " executed successfully."; } @GetMapping("/runTrans") public String runTrans(@RequestParam String transName) throws KettleException { kettleService.runTrans(transName); return "Trans " + transName + " executed successfully."; } } ``` 这样,就可以在Spring Boot中集成Kettle,并实现数据抽取、转换和加载的功能了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值