将xml数据从hive表字段移动到另一个hive表创建行格式serde

时间:2016-10-22 17:51:20

标签: hive

需要您帮助了解有关hive中xml数据的解决方案。

1)hive table" books_xml"只包含一个名为" xmldata"的字段我已将xml数据保存为单个字符串。

hive> select xmldata from books_xml;
OK
<?xml version="1.0" encoding="UTF-8"?><catalog><book>  <id>11</id> 
<genre>Computer</genre>  <price>44</price></book><book>  <id>44</id> 
<genre>Fantasy</genre>  <price>5</price></book></catalog>
Time taken: 0.175 seconds, Fetched: 1 row(s)

2)我想将上面的xml数据(在字段中)导出到使用SERDE属性创建的下面的hive表中?

CREATE TABLE books_serde (
   id STRING, 
   genre STRING, 
   price DOUBLE
)
ROW FORMAT SERDE 'com.ibm.spss.hive.serde2.xml.XmlSerDe'
WITH SERDEPROPERTIES (
"column.xpath.id"="/book/id/text()",
"column.xpath.genre"="/book/genre/text()",
"column.xpath.price"="/book/price/text()"
)
STORED AS
INPUTFORMAT 'com.ibm.spss.hive.serde2.xml.XmlInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.IgnoreKeyTextOutputFormat'
TBLPROPERTIES (
"xmlinput.start"="<book>",
"xmlinput.end"="</book>"
);

1 个答案:

答案 0 :(得分:0)

将第二个表创建为外部表,并指向第一个表的LOCATION

LOCATION /user/hive/warehouse/dbname/books_xml

这可以解决您的问题。