Kettle数据库配置抽离

泪湿孤枕 提交于 2020-05-04 09:56:50

在使用ETL工具Kettle时候,为了使作业或转换具有通用性,有时候,我们需要将数据库的连接配置从脚本或转换中抽离出来,下面介绍一种方案,该方案主要涉及的文件有:

# 这两个文件,默认是在系统的用户目录下,如果配置了KETTLE_HOME环境变量,则spoon会去KETTLE_HOME的目录下加载这两个文件
<USER_HOME>/.kettle/kettle.properties
<USER_HOME>/.kettle/shared.xml

共享数据库配置

在作业中,按照DB连接向导建立一个数据库连接,填入完整的数据库连接信息。

image-20181017182547003

image-20181017182954582

建立完连接,测试连接成功后,在该DB连接上点右键,选择“共享”。

image-20181017183142392

完成上述共享操作后,就可以在以下文件中找到共享数据库链接的配置:

<USER_HOME>/.kettle/shared.xml
<?xml version="1.0" encoding="UTF-8"?>
<sharedobjects>
  <connection>
    <name>test_db</name>
    <server>127.0.0.1</server>
    <type>MYSQL</type>
    <access>Native</access>
    <database>test_db</database>
    <port>3306</port>
    <username>testdb</username>
    <password>Encrypted 2be98afc86aa7f2e4cb79ff228dc6fa8c</password>
    <servername/>
    <data_tablespace/>
    <index_tablespace/>
    <attributes>
      <attribute><code>PORT_NUMBER</code><attribute>3306</attribute></attribute>
    </attributes>
  </connection>

</sharedobjects>

这样设置后,你就可以在后续的作业和转换中选择这个共享的test_db。

虽然这样算是抽离出了DB连接,但是还不够优化,下面介绍如何将DB连接中的部分配置再次抽离出来。

抽离DB配置

例如,我们想将数据库的地址、SID、username、password从shared.xml中抽离出来,这个时候,就需要用到

<USER_HOME>/.kettle/kettle.properties

我们只需要在这个文件中定义相关的DB变量,如下:

test_db_host=127.0.0.1
test_db_port=3306
test_db_sid=test_db
test_db_user=test_db
test_db_password=Encrypted 2be98afc86aa7f2e4cb79ff228dc6fa8c

然后,我们修改

<USER_HOME>/.kettle/shared.xml
<?xml version="1.0" encoding="UTF-8"?>
<sharedobjects>
  <connection>
    <name>test_db</name>
    <server>${test_db_host}</server>
    <type>MYSQL</type>
    <access>Native</access>
    <database>${test_db_sid}</database>
    <port>${test_db_port}</port>
    <username>${test_db_user}</username>
    <password>${test_db_password}</password>
    <servername/>
    <data_tablespace/>
    <index_tablespace/>
    <attributes>
      <attribute><code>PORT_NUMBER</code><attribute>${test_db_port}</attribute></attribute>
    </attributes>
  </connection>

</sharedobjects>

这样,就彻底的将数据库的配置从作业和转换中抽离出来了。

注意的一点是,最好在shared.xml中增加如下的数据库连接属性,可以有效避免数据传输字符、日期转换问题

<connection>
    <name>test_db</name>
    <server>${test_db_host}</server>
    <type>MYSQL</type>
    <access>Native</access>
    <database>${test_db_sid}</database>
    <port>${test_db_port}</port>
    <username>${test_db_user}</username>
    <password>${test_db_password}</password>
    <servername/>
    <data_tablespace/>
    <index_tablespace/>
    <attributes>
      <attribute>
        <code>EXTRA_OPTION_MYSQL.characterEncoding</code>
        <attribute>utf8</attribute>
      </attribute>
      <attribute>
        <code>FORCE_IDENTIFIERS_TO_LOWERCASE</code>
        <attribute>N</attribute>
      </attribute>
      <attribute>
        <code>FORCE_IDENTIFIERS_TO_UPPERCASE</code>
        <attribute>N</attribute>
      </attribute>
      <attribute>
        <code>IS_CLUSTERED</code>
        <attribute>N</attribute>
      </attribute>
      <attribute>
        <code>PORT_NUMBER</code>
        <attribute>${test_db_port}</attribute>
      </attribute>
      <attribute>
        <code>PRESERVE_RESERVED_WORD_CASE</code>
        <attribute>Y</attribute>
      </attribute>
      <attribute>
        <code>QUOTE_ALL_FIELDS</code>
        <attribute>N</attribute>
      </attribute>
      <attribute>
        <code>SQL_CONNECT</code>
        <attribute>set names utf8</attribute>
      </attribute>
      <attribute>
        <code>STREAM_RESULTS</code>
        <attribute>Y</attribute>
      </attribute>
      <attribute>
        <code>SUPPORTS_BOOLEAN_DATA_TYPE</code>
        <attribute>Y</attribute>
      </attribute>
      <attribute>
        <code>SUPPORTS_TIMESTAMP_DATA_TYPE</code>
        <attribute>Y</attribute>
      </attribute>
      <attribute>
        <code>USE_POOLING</code>
        <attribute>N</attribute>
      </attribute>
    </attributes>
  </connection>

做完上述的DB连接的共享和配置后,再处理作业和转换时,如果选择共享的DB连接,实际上Kettle会复制shared.xml中的数据库配置到具体的作业和转换中,复制过去后,相应的变量会从kettle.properties中读取。

也就是说,如果要把作业和转换发布到正式环境,实际上可以不需要shared.xml,shared.xml更多的作用是用于开发阶段。

另外,注意每次修改了shared和kettle.properties,要重启Kettle的开发工具,否则可能无法正常生效。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!