0

键值对组合。我们试图将 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"}]'

任何的意见都将会有帮助

4

1 回答 1

1

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

如果您必须从以下 XML 中捕获消息 id 以及 id 和 value

<?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)
于 2017-07-09T15:14:15.640 回答