kettle通过xml读取数据库连接

    以前总是把数据库连接直接设置在了ktr文件中,由于数据库连接的地址经常更换,就想通过xml来读取数据库连接,更换数据库连接时直接修改xml文件就行了。

这是我的数据库连接xml文件database.xml(以oracle为列):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Database>
 <URL>jdbc:oracle:thin:@192.168.1.100:1521:orcl</URL>
 <ClassName>oracle.jdbc.driver.OracleDriver</ClassName>
 <UserName>test</UserName>
 <Password>123456</Password>
</Database>

 然后读取这个xml文件,在读取xml的时候,注意数据类型:

kettle通过xml读取数据库连接_第1张图片

设定环境变量setVariable.ktr源码如下:

<?xml version="1.0" encoding="UTF-8"?>
<transformation>
  <info>
    <name>setVariable</name>
    <description/>
    <extended_description/>
    <trans_version/>
    <trans_status>0</trans_status>
    <directory>&#47;</directory>
    <parameters>
    </parameters>
    <log>
      <read/>
      <write/>
      <input/>
      <output/>
      <update/>
      <rejected/>
      <connection/>
      <table/>
      <step_performance_table/>
      <use_batchid>Y</use_batchid>
      <use_logfield>N</use_logfield>
    </log>
    <maxdate>
      <connection/>
      <table/>
      <field/>
      <offset>0.0</offset>
      <maxdiff>0.0</maxdiff>
    </maxdate>
    <size_rowset>10000</size_rowset>
    <sleep_time_empty>50</sleep_time_empty>
    <sleep_time_full>50</sleep_time_full>
    <unique_connections>N</unique_connections>
    <feedback_shown>Y</feedback_shown>
    <feedback_size>50000</feedback_size>
    <using_thread_priorities>Y</using_thread_priorities>
    <shared_objects_file/>
    <capture_step_performance>N</capture_step_performance>
    <step_performance_capturing_delay>1000</step_performance_capturing_delay>
    <dependencies>
    </dependencies>
    <partitionschemas>
    </partitionschemas>
    <slaveservers>
    </slaveservers>
    <clusterschemas>
    </clusterschemas>
  <modified_user>-</modified_user>
  <modified_date>2008&#47;04&#47;29 14:56:48.203</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
  <hop> <from>获取系统设定日期</from><to>设定变量</to><enabled>Y</enabled> </hop>  <hop> <from>GetdataBaseXML</from><to>获取系统设定日期</to><enabled>Y</enabled> </hop>  </order>
  <step>
    <name>GetdataBaseXML</name>
    <type>getXMLData</type>
    <description/>
    <distribute>Y</distribute>
    <copies>1</copies>
         <partitioning>
           <method>none</method>
           <schema_name/>
           </partitioning>
    <include>N</include>
    <include_field/>
    <rownum>N</rownum>
    <addresultfile>N</addresultfile>
    <namespaceaware>N</namespaceaware>
    <ignorecomments>N</ignorecomments>
    <readurl>N</readurl>
    <validating>N</validating>
    <usetoken>N</usetoken>
    <IsIgnoreEmptyFile>N</IsIgnoreEmptyFile>
    <doNotFailIfNoFile>Y</doNotFailIfNoFile>
    <rownum_field/>
    <encoding>UTF-8</encoding>
    <file>
      <name>${Internal.Transformation.Filename.Directory}&#47;dataBase.xml</name>
      <filemask/>
      <file_required>N</file_required>
    </file>
    <fields>
      <field>
        <name>URL</name>
        <xpath>URL</xpath>
        <element_type>node</element_type>
        <type>String</type>
        <format/>
        <currency/>
        <decimal/>
        <group/>
        <length>-1</length>
        <precision>-1</precision>
        <trim_type>none</trim_type>
        <repeat>N</repeat>
      </field>
      <field>
        <name>ClassName</name>
        <xpath>ClassName</xpath>
        <element_type>node</element_type>
        <type>String</type>
        <format/>
        <currency/>
        <decimal/>
        <group/>
        <length>-1</length>
        <precision>-1</precision>
        <trim_type>none</trim_type>
        <repeat>N</repeat>
      </field>
      <field>
        <name>UserName</name>
        <xpath>UserName</xpath>
        <element_type>node</element_type>
        <type>String</type>
        <format/>
        <currency/>
        <decimal/>
        <group/>
        <length>-1</length>
        <precision>-1</precision>
        <trim_type>none</trim_type>
        <repeat>N</repeat>
      </field>
      <field>
        <name>Password</name>
        <xpath>Password</xpath>
        <element_type>node</element_type>
        <type>String</type>
        <format/>
        <currency/>
        <decimal/>
        <group/>
        <length>-1</length>
        <precision>-1</precision>
        <trim_type>none</trim_type>
        <repeat>N</repeat>
      </field>
    </fields>
    <limit>0</limit>
    <loopxpath>&#47;Database</loopxpath>
    <IsInFields>N</IsInFields>
    <IsAFile>N</IsAFile>
    <XmlField/>
    <prunePath/>
     <cluster_schema/>
 <remotesteps>   <input>   </input>   <output>   </output> </remotesteps>    <GUI>
      <xloc>80</xloc>
      <yloc>71</yloc>
      <draw>Y</draw>
      </GUI>
    </step>

  <step>
    <name>获取系统设定日期</name>
    <type>SystemInfo</type>
    <description/>
    <distribute>Y</distribute>
    <copies>1</copies>
         <partitioning>
           <method>none</method>
           <schema_name/>
           </partitioning>
    <fields>
      <field>
        <name>begintime</name>
        <type>command line argument 1</type>
        </field>
      <field>
        <name>yesterday</name>
        <type>command line argument 2</type>
        </field>
      <field>
        <name>today</name>
        <type>command line argument 3</type>
        </field>
      </fields>
     <cluster_schema/>
 <remotesteps>   <input>   </input>   <output>   </output> </remotesteps>    <GUI>
      <xloc>276</xloc>
      <yloc>72</yloc>
      <draw>Y</draw>
      </GUI>
    </step>

  <step>
    <name>设定变量</name>
    <type>SetVariable</type>
    <description/>
    <distribute>Y</distribute>
    <copies>1</copies>
         <partitioning>
           <method>none</method>
           <schema_name/>
           </partitioning>
    <fields>
      <field>
        <field_name>URL</field_name>
        <variable_name>URL</variable_name>
        <variable_type>ROOT_JOB</variable_type>
        <default_value/>
        </field>
      <field>
        <field_name>ClassName</field_name>
        <variable_name>ClassName</variable_name>
        <variable_type>ROOT_JOB</variable_type>
        <default_value/>
        </field>
      <field>
        <field_name>UserName</field_name>
        <variable_name>UserName</variable_name>
        <variable_type>ROOT_JOB</variable_type>
        <default_value/>
        </field>
      <field>
        <field_name>Password</field_name>
        <variable_name>Password</variable_name>
        <variable_type>ROOT_JOB</variable_type>
        <default_value/>
        </field>
      <field>
        <field_name>begintime</field_name>
        <variable_name>begintime</variable_name>
        <variable_type>ROOT_JOB</variable_type>
        <default_value/>
        </field>
      <field>
        <field_name>yesterday</field_name>
        <variable_name>yesterday</variable_name>
        <variable_type>ROOT_JOB</variable_type>
        <default_value/>
        </field>
      <field>
        <field_name>today</field_name>
        <variable_name>today</variable_name>
        <variable_type>ROOT_JOB</variable_type>
        <default_value/>
        </field>
      </fields>
     <cluster_schema/>
 <remotesteps>   <input>   </input>   <output>   </output> </remotesteps>    <GUI>
      <xloc>463</xloc>
      <yloc>68</yloc>
      <draw>Y</draw>
      </GUI>
    </step>

  <step_error_handling>
  </step_error_handling>
   <slave-step-copy-partition-distribution>
</slave-step-copy-partition-distribution>
   <slave_transformation>N</slave_transformation>
</transformation>
要想使用这样的数据库连接,在写一个需要数据库连接的ktr时,需要选择Generic database,如下图所示:

kettle通过xml读取数据库连接_第2张图片

这样我们在做job的时候,就可以先执行setVariable.ktr,然后执行连接数据的ktr就可以了。

不过我在测试中发现,不是所有的ktr都可以,有的可以,有的却不可以,具体是什么原因,还没有找到!

还需要进一步的测试!!!

你可能感兴趣的:(数据库,xml,schema,performance,dependencies,encoding)