在HiveXMLSerde中使用“属性到属性”Map

c2e8gylq  于 2021-06-01  发布在  Hadoop
关注(0)|答案(1)|浏览(347)

我有一个如下所示的xml文档:

<root>
 <unwanted>
  ...
 </unwanted>
 <wanted version="A">
  <unwanted2 type='1'>
   ...
  </unwanted2>
  <unwanted2 type='2'>
   ...
  </unwanted2>
  <unwanted2 type='3'>
   ...
  </unwanted2>
  <wanted2>
   <detail>
    <row date="Jan-17" price="100" inventory="50">
    <row date="Feb-17" price="101" inventory="40">
    <row date="Mar-17" price="102" inventory="30">
   </detail>
  </wanted2>
 </wanted>
 <wanted version="B">
  <unwanted2 type='1'>
   ...
  </unwanted2>
  <unwanted2 type='2'>
   ...
  </unwanted2>
  <unwanted2 type='3'>
   ...
  </unwanted2>
  <wanted2>
   <detail>
    <row date="Jan-17" price="200" inventory="60">
    <row date="Feb-17" price="201" inventory="70">
    <row date="Mar-17" price="202" inventory="80">
   </detail>
  </wanted2>
 </wanted>
</root>

我想将文件导入 Hive 表,最好是这种格式:

Version | Date   | Price | Inventory
A         Jan-17   100     50
A         Feb-17   101     40
A         Mar-17   102     30
B         Jan-17   200     60
B         Feb-17   201     70
B         Mar-17   202     80

但我现在愿意把它作为日期和价格的Map输入:

version | spot_date
A         {Date: Jan-17, Price: 100, Inventory: 50}
A         {Date: Feb-17, ...}
A         {Date: Mar-17, ...}
B         {Date: Jan-17, ...}
B         {Date: Feb-17, ...}
B         {Date: Mar-17, ...}

我正在尝试将xmlserde用于配置单元,并使用“属性到属性”特性。
我的查询如下所示:

CREATE EXTERNAL TABLE ppa_test(
    version        STRING, 
    spot_date      MAP<STRING,STRING>
)
ROW FORMAT SERDE 'com.ibm.spss.hive.serde2.xml.XmlSerDe'
WITH SERDEPROPERTIES (
    "column.xpath.version"="/wanted/@version",
    "column.xpath.spot_date"="/wanted/wanted2/detail/row",
    "xml.map.specification.row"="date->@date"
)
STORED AS
INPUTFORMAT 'com.ibm.spss.hive.serde2.xml.XmlInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.IgnoreKeyTextOutputFormat'
TBLPROPERTIES (
"xmlinput.start"="<wanted ",
"xmlinput.end"="</wanted>"
);

但当我加载数据时,我得到:

version | spot_date
A         {"row":"Mar-17"}
B         {"row":"Mar-17"}

如果我改成 xml.map.spec 路径:

"xml.map.specification.row"="@date->@price"

我可以分别读取xml的每一行,但它记录在同一个配置单元表行中,而且我更希望使用属性名:

Version | spot_date
A         {"Mar-17":"102", "Feb-17":"101", "Jan-17":"100"}
B         {"Mar-17":"202", "Feb-17":"201", "Jan-17":"200"}

如何记录每个xml row 节点到其自己的配置单元记录中
如何使用属性名(或自定义字符串)作为键?
编辑
所以从 spot_date MAP<STRING,STRING> 到。。。

CREATE EXTERNAL TABLE ppa_test(
    scenario    STRING, 
    spot_date   array<struct<
        date:      string, 
        price:     string, 
        inventory: string, 
    >>
)...

给我一个对象数组

Version | spot_date
A         [{date: Jan-17, price: 100, inventory: 50},
           {date: Feb-17, price: 101, inventory: 40},
           {date: Mar-17, price: 102, inventory: 30}]
B         [{date: Jan-17, ... ]

从上面完成了#2,但仍然不确定#1

uxhixvfz

uxhixvfz1#

可以分解为#2创建的结构数组,得到#1。

CREATE EXTERNAL TABLE ppa_test(
    scenario    STRING, 
    spot_date   ARRAY<STRUCT<spotdates: struct<
        date:      string, 
        price:     string, 
        inventory: string, 
    >>>
)

你可以用侧视图

DROP TABLE IF EXISTS ppa_test_exploded;
CREATE TABLE ppa_test_exploded as
 SELECT scenario,
  SD.spotdates.date as date,
  SD.spotdates.price as price,
  SD.spotdates.inventory as inventory
  FROM ppa_test
  LATERAL VIEW EXPLODE(spot_date) exploded as SD;

希望这有帮助。

相关问题