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先编写好

五、测试

说明:工具类简单测试

在这里插入图片描述

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot是一个快速开发框架,而Kettle是一个强大的ETL工具。将它们整合在一起可以实现数据的快速导入和导出。 首先,需要在pom.xml文件中添加Kettle的依赖: ``` <dependency> <groupId>org.pentaho</groupId> <artifactId>kettle-core</artifactId> <version>8.3..-371</version> </dependency> ``` 然后,在Spring Boot的配置文件中添加Kettle的配置: ``` kettle: home: /opt/kettle repository: /opt/kettle/repo ``` 其中,kettle.home是Kettle的安装目录,kettle.repository是Kettle的资源库目录。 接下来,可以在Spring Boot的代码中使用Kettle的API来实现数据的导入和导出。例如: ``` @Autowired private KettleEnvironment kettleEnvironment; public void importData() throws KettleException { kettleEnvironment.init(); KettleEnvironment.setExecutionConfiguration(null); TransMeta transMeta = new TransMeta("/opt/kettle/trans/import.ktr"); Trans trans = new Trans(transMeta); trans.execute(null); trans.waitUntilFinished(); } public void exportData() throws KettleException { kettleEnvironment.init(); KettleEnvironment.setExecutionConfiguration(null); JobMeta jobMeta = new JobMeta("/opt/kettle/job/export.kjb", null); Job job = new Job(null, jobMeta); job.start(); job.waitUntilFinished(); } ``` 以上代码分别实现了数据的导入和导出,其中import.ktr和export.kjb是Kettle的转换和作业文件。 最后,需要注意的是,Kettle的API在使用时需要先初始化Kettle环境,否则会报错。因此,在Spring Boot的启动类中需要添加以下代码: ``` @SpringBootApplication public class Application implements CommandLineRunner { @Autowired private KettleEnvironment kettleEnvironment; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... args) throws Exception { kettleEnvironment.init(); } } ``` 这样,就完成了Spring Boot整合Kettle的配置和使用。 ### 回答2: Kettle是一个ETL(Extract, Transform, Load)工具,也是PDI(Pentaho Data Integration)的核心组件之一。Spring Boot是一个流行的开发框架,它可以大大简化应用程序的开发过程。Spring Boot和Kettle是两个不同的工具,但是它们可以很好地整合在一起,让开发人员更容易地实现数据处理任务。 下面是Spring Boot整合Kettle的几个关键步骤: 1. 导入Kettle依赖 在Spring Boot项目的pom.xml文件中,添加Kettle的依赖。可以在Maven中央仓库中找到这些依赖项,例如: ``` <dependency> <groupId>org.pentaho</groupId> <artifactId>kettle-core</artifactId> <version>9.1.0.0-324</version> </dependency> <dependency> <groupId>org.pentaho</groupId> <artifactId>kettle-ui-swt</artifactId> <version>9.1.0.0-324</version> </dependency> ``` 2. 配置Kettle 在Spring Boot项目的配置文件中,添加Kettle的相关配置。例如,可以设置Kettle的安装路径、数据库连接信息等。以下是一个示例配置: ``` kettle.home=C:/kettle kettle.db.driver=org.postgresql.Driver kettle.db.url=jdbc:postgresql://localhost/test kettle.db.user=postgres kettle.db.pass=password kettle.db.schema=public ``` 3. 编写Kettle任务 在Spring Boot项目中,可以编写Kettle任务。可以使用Kettle自带的设计器(Spoon)创建任务,也可以使用代码编写任务。以下是一个示例任务: ``` <transformation> <info> <name>example_transform</name> </info> <step> <name>input</name> <type>TextFileInput</type> <description>Read input text file</description> <filename>input.txt</filename> <delimiter>,</delimiter> <enclosure>"</enclosure> <header>Y</header> <lazy_conversion>Y</lazy_conversion> <dateFormatLenient>Y</dateFormatLenient> <dateFormatLocale></dateFormatLocale> </step> <step> <name>cleanse</name> <type>FilterRows</type> <description>Cleanse invalid data</description> <condition>NOT( [field1] IS NULL OR [field2] IS NULL )</condition> </step> <step> <name>output</name> <type>TextFileOutput</type> <description>Write output text file</description> <filename>output.txt</filename> <delimiter>,</delimiter> <enclosure>"</enclosure> <header>Y</header> <splitEvery>-1</splitEvery> </step> <hop> <from>input</from> <to>cleanse</to> </hop> <hop> <from>cleanse</from> <to>output</to> </hop> </transformation> ``` 4. 执行Kettle任务 在Spring Boot项目中,可以使用Kettle的Java API执行任务。以下是一个示例代码: ``` @Configuration public class KettleConfig { @Autowired private Environment env; @Bean public KettleEnvironment kettleEnvironment() throws KettleException { KettleEnvironment.init(); return KettleEnvironment.getInstance(); } @Bean public DatabaseMeta databaseMeta() { String driver = env.getProperty("kettle.db.driver"); String url = env.getProperty("kettle.db.url"); String user = env.getProperty("kettle.db.user"); String pass = env.getProperty("kettle.db.pass"); String schema = env.getProperty("kettle.db.schema"); DatabaseMeta dbMeta = new DatabaseMeta(); dbMeta.setDatabaseType("PostgreSQL"); dbMeta.setAccessType(DatabaseMeta.TYPE_ACCESS_JNDI); dbMeta.setName("test"); dbMeta.addExtraOption("INITIAL_POOL_SIZE", "10"); dbMeta.addExtraOption("MAXIMUM_POOL_SIZE", "100"); dbMeta.setJdbcDriver(driver); dbMeta.setDBName(url); dbMeta.setUsername(user); dbMeta.setPassword(pass); dbMeta.setPreferredSchemaName(schema); return dbMeta; } @Bean public Trans trans(DatabaseMeta databaseMeta) throws KettleException { KettleEnvironment.init(); TransMeta transMeta = new TransMeta("example_transform.ktr"); transMeta.setUsingUniqueConnections(true); transMeta.setMetaStore(KettleUtil.getMetaStore()); Trans trans = new Trans(transMeta); trans.setLogLevel(LogLevel.BASIC); trans.setMetaStore(KettleUtil.getMetaStore()); trans.setDatabaseMeta(databaseMeta); return trans; } } ``` 5. 监控任务执行 在Spring Boot项目中,可以编写代码来监控Kettle任务的执行情况,并生成相应的日志。可以使用Kettle的日志功能来记录任务执行过程中的错误和警告信息。以下是一个示例代码: ``` @Autowired private Trans trans; public void execute() throws KettleException { trans.execute(null); Result result = trans.getResult(); String status = result != null && result.getNrErrors() == 0 ? "SUCCESS" : "FAILED"; log.info("Kettle job execution {}.", status); if (result != null && result.getNrErrors() > 0) { for (LogMessage message : result.getLogChannel().getLogBuffer() ) { if (message.isError()) { log.error(message.getMessage()); } else if ( message.isWarning() ) { log.warn( message.getMessage() ); } else { log.info( message.getMessage() ); } } } } ``` 总之,Spring Boot和Kettle整合可以极大地简化数据处理任务的开发流程。通过采用KettleETL工具和Spring Boot的开发框架,开发人员可以更加高效地处理数据。同时,Kettle的强大功能也可以为Spring Boot项目提供更加丰富和灵活的数据处理能力。 ### 回答3: 随着数据量的不断增长,数据处理变得越来越重要,kettle是一款著名的数据处理工具,可以用于数据清洗、转换、集成等多种操作。而SpringBoot作为一款开发框架,充分发挥其快速开发的优势,简化了应用程序的配置和部署过程,也受到了众多Java开发者的追捧。因此,SpringBoot整合Kettle就成为了一种趋势。 SpringBootKettle整合的优点: (1)Kettle具有独立的编写、组装和执行ETL任务的能力,在大数据处理和企业数据仓库建设中使用广泛。这使得Kettle对于大规模数据处理来说是必不可少的。而SpringBoot作为一款快速开发的框架,能够简化开发流程,提高开发效率。 (2)Kettle支持的数据源和目标可以是各种数据库、CSV、XML等等,SpringBoot能够方便的处理数据源,和Kettle完美结合,可以轻松地完成ETL工作流的设计、发布和操作,从而实现数据的整合、转换和清洗。 (3)SpringBoot整合Kettle过程中,可以使用Spring Security,对于Kettle执行作业等操作进行权限访问控制,有效地保护数据安全。 SpringBoot整合Kettle的具体实现: (1)首先,需要在Maven中添加SpringBootKettle的相关依赖: <dependency> <groupId>org.pentaho</groupId> <artifactId>kettle-core</artifactId> <version>9.1.0.0-324</version> </dependency> <dependency> <groupId>org.pentaho</groupId> <artifactId>kettle-engine</artifactId> <version>9.1.0.0-324</version> </dependency> (2)其次,可以使用Kettle自带的Job和Transformation的API,通过编写Java代码实现Kettle的操作、执行等功能。 (3)如果要实现数据源的多样化,可以使用Spring Data,通过配置数据源的方式,实现与不同数据库的数据交互。 (4)为了防止数据的泄露,可以使用Spring Security来保护Kettle的资源,并控制用户权限。 总之,通过SpringBoot整合Kettle,能够实现快速开发、方便部署、易于维护等优点,实现数据清洗、转换、整合等多种操作,满足企业业务需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值