2017-07-07 67 views
0

鍵值對組合。我們試圖將ID作爲列名和VALUE分解爲每列的相應數據。配置單元 - XML-串行 - 鍵值對 - 創建表

`<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 

我們嘗試使用地圖提取鍵值對,但我們沒有得到想要的結果。

'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"}]' 

任何建議將是有益的

+0

不會發生。列應該事先知道。 –

+0

謝謝你的迴應。如果我們知道列並使用期望的列創建表,我們可以將它的值部分映射到相應的列嗎? – Paciferous

回答

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)