如何使用Serde通过Hive解析xml数据?

时间:2019-03-19 14:01:51

标签: hive hive-serde

我有一个.xml文件,其数据如下:

<book>
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <genre>Computer</genre>
    <price discount="0.15">44.95</price>
    <publish_date>2000-10-01</publish_date>
    <description>An in-depth look at creating applications with XML.</description>
</book>
<book>
    <author>Knorr, Stefan</author>
    <title>Creepy Crawlies</title>
    <genre>Horror</genre>
    <price discount="0.15">4.95</price>
    <publish_date>2000-12-06</publish_date>
    <description>An anthology of horror stories about roaches,centipedes, scorpionsand other insects.</description>
</book>
<book>
    <author>Galos, Mike</author>
    <title>Visual Studio 7: A Comprehensive Guide</title>
    <genre>Computer</genre>
    <price discount="0.15">49.95</price>
    <publish_date>2001-04-16</publish_date>
    <description>Microsoft Visual Studio 7 is explored in depth, looking at how Visual Basic, Visual C++, C#, and ASP+ are integrated into a comprehensive development environment.</description>
</book>

我试图通过使用Serde在hdfs上的xml文件顶部创建外部表来通过蜂巢解析xml。请在下面找到我的代码

我首先添加了罐子

add jar hdfs://xtlinno1vftsnxg:8020/user/hdfs/hivexmlserde-1.0.5.3.jar;

CREATE EXTERNAL TABLE hive_test_xml(
. . . . . . . . . . . . . . . . . . . . . . .> col1            string,
. . . . . . . . . . . . . . . . . . . . . . .> col2            string,
. . . . . . . . . . . . . . . . . . . . . . .> col3            string)
. . . . . . . . . . . . . . . . . . . . . . .> ROW FORMAT SERDE 'com.ibm.spss.hive.serde2.xml.XmlSerDe'
. . . . . . . . . . . . . . . . . . . . . . .> WITH SERDEPROPERTIES (
. . . . . . . . . . . . . . . . . . . . . . .> "column.xpath.col1"="/book/author/text()",
. . . . . . . . . . . . . . . . . . . . . . .> "column.xpath.col2"="/book/title/text()",
. . . . . . . . . . . . . . . . . . . . . . .> "column.xpath.col3"="/book/genre/text()"
. . . . . . . . . . . . . . . . . . . . . . .> )
. . . . . . . . . . . . . . . . . . . . . . .> STORED AS
. . . . . . . . . . . . . . . . . . . . . . .> INPUTFORMAT 'com.ibm.spss.hive.serde2.xml.XmlInputFormat'
. . . . . . . . . . . . . . . . . . . . . . .> OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.IgnoreKeyTextOutputFormat'
. . . . . . . . . . . . . . . . . . . . . . .> LOCATION 'hdfs://xtlinno1vftsnxg:8020/user/poctest2/testxml2.xml'
. . . . . . . . . . . . . . . . . . . . . . .> TBLPROPERTIES (
. . . . . . . . . . . . . . . . . . . . . . .> "xmlinput.start"="<book",
. . . . . . . . . . . . . . . . . . . . . . .> "xmlinput.end"="</book>");

我遇到的错误是

  

执行错误,返回代码1   org.apache.hadoop.hive.ql.exec.DDLTask。   org / apache / hadoop / hive / serde2 / SerDe(state = 08S01,code = 1)

我不确定如何解决此错误。请帮助!

1 个答案:

答案 0 :(得分:0)

metastore_db很可能已损坏。将验证设置为忽略并修复metastore中的表名。

set hive.msck.path.validation=ignore;
MSCK REPAIR TABLE table_name;