将行转换为pentaho中的标题

时间:2017-03-23 12:45:49

标签: pentaho pentaho-spoon pentaho-data-integration

我有一个如下所述的文件

FIELD1,FIELD2
name,ABC
age,29
location,ZZ
name,XYZ
age,33
location,YY

我要求输出如下。我尝试了行非规范化,但没有给出正确的输出

name,age,location
ABC,29,ZZ
XYZ,33,YY

1 个答案:

答案 0 :(得分:3)

行反规范化器可以生成这样的输出,但它需要输入中每个实体的一些标识符。输入必须按此标识符排序。

因此,您首先需要将文件的输出转换为以下结构:

ID,FIELD1,FIELD2
0,name,ABC
0,age,29
0,location,ZZ
1,name,XYZ
1,age,33
1,location,YY

实现此目的的一种方法是将Add sequence(从0开始)和User Defined Java Expression(将表达式设置为ID / 3组合,如果您总是有三行对应于同一实体)。

然后您可以使用Row denormalizer,如下图所示。

您的转型将如下所示:

enter image description here

转换步骤xml(只需将其复制并粘贴到转换画布上):

<?xml version="1.0" encoding="UTF-8"?>
<transformation-steps>
<steps>
  <step>
    <name>Data Grid</name>
    <type>DataGrid</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
         <partitioning>
           <method>none</method>
           <schema_name/>
           </partitioning>
    <fields>
      <field>
        <name>FIELD1</name>
        <type>String</type>
        <format/>
        <currency/>
        <decimal/>
        <group/>
        <length>-1</length>
        <precision>-1</precision>
        <set_empty_string>N</set_empty_string>
      </field>
      <field>
        <name>FIELD2</name>
        <type>String</type>
        <format/>
        <currency/>
        <decimal/>
        <group/>
        <length>-1</length>
        <precision>-1</precision>
        <set_empty_string>N</set_empty_string>
      </field>
    </fields>
    <data>
      <line> <item>name</item><item>ABC</item> </line>
      <line> <item>age</item><item>29</item> </line>
      <line> <item>location</item><item>ZZ</item> </line>
      <line> <item>name</item><item>XYZ</item> </line>
      <line> <item>age</item><item>33</item> </line>
      <line> <item>location</item><item>YY</item> </line>
    </data>
     <cluster_schema/>
 <remotesteps>   <input>   </input>   <output>   </output> </remotesteps>    <GUI>
      <xloc>128</xloc>
      <yloc>64</yloc>
      <draw>Y</draw>
      </GUI>
    </step>

  <step>
    <name>Row denormaliser</name>
    <type>Denormaliser</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
         <partitioning>
           <method>none</method>
           <schema_name/>
           </partitioning>
      <key_field>FIELD1</key_field>
      <group>
        <field>
          <name>ID</name>
          </field>
        </group>
      <fields>
        <field>
          <field_name>FIELD2</field_name>
          <key_value>name</key_value>
          <target_name>name</target_name>
          <target_type>String</target_type>
          <target_format/>
          <target_length>-1</target_length>
          <target_precision>-1</target_precision>
          <target_decimal_symbol/>
          <target_grouping_symbol/>
          <target_currency_symbol/>
          <target_null_string/>
          <target_aggregation_type>-</target_aggregation_type>
          </field>
        <field>
          <field_name>FIELD2</field_name>
          <key_value>age</key_value>
          <target_name>age</target_name>
          <target_type>Integer</target_type>
          <target_format/>
          <target_length>-1</target_length>
          <target_precision>-1</target_precision>
          <target_decimal_symbol/>
          <target_grouping_symbol/>
          <target_currency_symbol/>
          <target_null_string/>
          <target_aggregation_type>-</target_aggregation_type>
          </field>
        <field>
          <field_name>FIELD2</field_name>
          <key_value>location</key_value>
          <target_name>location</target_name>
          <target_type>String</target_type>
          <target_format/>
          <target_length>-1</target_length>
          <target_precision>-1</target_precision>
          <target_decimal_symbol/>
          <target_grouping_symbol/>
          <target_currency_symbol/>
          <target_null_string/>
          <target_aggregation_type>-</target_aggregation_type>
          </field>
        </fields>
     <cluster_schema/>
 <remotesteps>   <input>   </input>   <output>   </output> </remotesteps>    <GUI>
      <xloc>672</xloc>
      <yloc>64</yloc>
      <draw>Y</draw>
      </GUI>
    </step>

  <step>
    <name>Add sequence</name>
    <type>Sequence</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
         <partitioning>
           <method>none</method>
           <schema_name/>
           </partitioning>
      <valuename>ID</valuename>
      <use_database>N</use_database>
      <connection/>
      <schema/>
      <seqname>SEQ_</seqname>
      <use_counter>Y</use_counter>
      <counter_name/>
      <start_at>0</start_at>
      <increment_by>1</increment_by>
      <max_value>999999999</max_value>
     <cluster_schema/>
 <remotesteps>   <input>   </input>   <output>   </output> </remotesteps>    <GUI>
      <xloc>272</xloc>
      <yloc>64</yloc>
      <draw>Y</draw>
      </GUI>
    </step>

  <step>
    <name>User Defined Java Expression</name>
    <type>Janino</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
         <partitioning>
           <method>none</method>
           <schema_name/>
           </partitioning>
       <formula><field_name>ID</field_name>
<formula_string>ID &#x2f; 3</formula_string>
<value_type>Integer</value_type>
<value_length>-1</value_length>
<value_precision>-1</value_precision>
<replace_field>ID</replace_field>
</formula>
     <cluster_schema/>
 <remotesteps>   <input>   </input>   <output>   </output> </remotesteps>    <GUI>
      <xloc>432</xloc>
      <yloc>64</yloc>
      <draw>Y</draw>
      </GUI>
    </step>

</steps>
<order>
  <hop> <from>Data Grid</from><to>Add sequence</to><enabled>Y</enabled> </hop>
  <hop> <from>Add sequence</from><to>User Defined Java Expression</to><enabled>Y</enabled> </hop>
  <hop> <from>User Defined Java Expression</from><to>Row denormaliser</to><enabled>Y</enabled> </hop>
</order>
<notepads>
</notepads>
<step_error_handling>
</step_error_handling>
</transformation-steps>

最后,如果您需要,可以使用Select values步骤删除ID列。