springboot 整合 kettle 坑点

需要引入的jar:缺其中一个就会报一堆奇奇怪怪的错

1.注意放到lib的位置,后面会用上。

2.jar从kettle客户端里扒拉来

3.我这边是用kettle从oracle转到mySQL做增量同步,所以要ojdbc8,mysql5.1.49,用5.1.49注意mysql数据库mysql配置要用driver-class-name: com.mysql.jdbc.Driver

 

pom的写法:注意<build>里面的写法,这样后面打包后的外部jar包才会跟着过去,不然就会部署后运行报错缺少jar包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!--排除默认的tomcat-jdbc-->
                <exclusion>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>5.1.49</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.5.13</version>
        </dependency>


        <dependency>
            <groupId>org.pentaho.di</groupId>
            <artifactId>kettle-core</artifactId>
            <version>8.2.0.0-342</version>
            <scope>system</scope>
            <systemPath>${basedir}/lib/kettle-core-8.2.0.0-342.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>org.pentaho.di</groupId>
            <artifactId>kettle-engine</artifactId>
            <version>8.2.0.0-342</version>
            <scope>system</scope>
            <systemPath>${basedir}/lib/kettle-engine-8.2.0.0-342.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.46</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
            <scope>system</scope>
            <systemPath>${basedir}/lib/commons-lang-2.6.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>oracle</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>8.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/lib/ojdbc8.jar</systemPath>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>mysql</groupId>-->
<!--            <artifactId>mysql-connector-java</artifactId>-->
<!--            <version>5.1.49</version>-->
<!--            <scope>system</scope>-->
<!--            <systemPath>${project.basedir}/src/main/resources/lib/mysql-connector-java-5.1.49.jar</systemPath>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.pentaho.metastore</groupId>
            <artifactId>metastore</artifactId>
            <version>8.2.0.0-342</version>
            <scope>system</scope>
            <systemPath>${basedir}/lib/metastore-8.2.0.0-342.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-vfs2</artifactId>
            <version>2.2</version>
            <scope>system</scope>
            <systemPath>${basedir}/lib/commons-vfs2-2.2.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
            <scope>system</scope>
            <systemPath>${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>${basedir}/lib/commons-io-2.2.jar</systemPath>
        </dependency>
        <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2.2</version>
        <scope>system</scope>
        <systemPath>${basedir}/lib/commons-collections-3.2.2.jar</systemPath>
    </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>17.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/lib/guava-17.0.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>lib</directory>
                <targetPath>/BOOT-INF/lib/</targetPath>
                <includes>
                    <include>**/*.jar</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>

 

### 回答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)工具,也是PDIPentaho Data Integration)的核心组件之一。Spring Boot是一个流行的开发框架,它可以大大简化应用程序的开发过程。Spring BootKettle是两个不同的工具,但是它们可以很好地整合在一起,让开发人员更容易地实现数据处理任务。 下面是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. 配置KettleSpring 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 BootKettle整合可以极大地简化数据处理任务的开发流程。通过采用Kettle的ETL工具和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、付费专栏及课程。

余额充值