Hive - XML- Serde - 键值对 - 创建表

时间:2017-07-07 16:59:57

标签: xml hadoop xpath hive hive-serde

键值对组合。我们试图将ID作为列名称和VALUE作为每列的相应数据进行爆炸。

`<CT> <items> <item> <field> <id>Column1</id> <value>25672</value> </field> 
 <field> <id>Column2</id> <value>FGE</value> </field> <field> 
 <id>Column3</id> <value>Florence to Venice</value> </field> </item> 
 </items> 
 </CT>`

我们期待创建如下表格, 预期产出:

Column1 Column2 Column3
25672   FGE     Florence to Venice

我们尝试使用Map来提取键值对,但我们没有得到所需的结果。

'CREATE EXTERNAL TABLE dev.reference_test(
PM_SubCollection array<map<string,string>>
)
ROW FORMAT SERDE 'com.ibm.spss.hive.serde2.xml.XmlSerDe'
WITH SERDEPROPERTIES(
"column.xpath.PM_SubCollection"="/CT/items/item/field",
"xml.map.specification.id"="#id->#value"
)
STORED AS
INPUTFORMAT 'com.ibm.spss.hive.serde2.xml.XmlInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.IgnoreKeyTextOutputFormat'
LOCATION '/dev/reference_test'
TBLPROPERTIES (
"xmlinput.start"="",
"xmlinput.end"=""
);'


Output:
'[{"field":"Column125672"},{"field":"Column2FGE"},{"field":"Column3Florence to Venice"}]'

任何建议都会有所帮助

1 个答案:

答案 0 :(得分:1)

请参阅https://github.com/dvasilen/Hive-XML-SerDe/issues/42

如果必须从以下XML

捕获消息ID以及id和值
<?xml version="1.0" encoding="UTF-8"?>
<CT>
   <messageID>11736</messageID>
   <items>
      <item>
         <field>
            <id>Column1</id>
            <value>25672</value>
         </field>
         <field>
            <id>Column2</id>
            <value>FGE</value>
         </field>
         <field>
            <id>Column3</id>
            <value>Florence to Venice</value>
         </field>
      </item>
   </items>
</CT>

然后我会选择看起来像这样的DDL:

DROP TABLE IF EXISTS xml_42a;

CREATE  TABLE xml_42a(
message_id string,
fields array<struct<field:struct<id:string,value:string>>>
)
ROW FORMAT SERDE 'com.ibm.spss.hive.serde2.xml.XmlSerDe'
WITH SERDEPROPERTIES(
  "column.xpath.message_id"="/CT/messageID/text()",
  "column.xpath.fields"="/CT/items/item/field"
  )
STORED AS 
INPUTFORMAT 'com.ibm.spss.hive.serde2.xml.XmlInputFormat' 
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.IgnoreKeyTextOutputFormat' 
TBLPROPERTIES (
"xmlinput.start"="<CT>",
"xmlinput.end"="</CT>"
);

load data local inpath '/Users/dvasilen/Misc/XML/42a.xml' OVERWRITE into table xml_42a;

select * from xml_42a; 

这是输出:

hive> 
    > select * from xml_42a; 
OK
11736   [{"field":{"id":"Column1","value":"25672"}},{"field":{"id":"Column2","value":"FGE"}},{"field":{"id":"Column3","value":"Florence to Venice"}}]
Time taken: 0.08 seconds, Fetched: 1 row(s)