2013-12-10 36 views
2

我有以下輸入XML:蜂箱 - 側視爆炸的XPath

<?xml version="1.0"?> 
<Employees> 
    <Employee emplid="1111"> 
     <lastname>Watson</lastname> 
     <age>30</age> 
     <email>[email protected]</email> 
    </Employee> 
    <Employee emplid="2222"> 
     <firstname>Sherlock</firstname> 
     <lastname>Holmes</lastname> 
     <age>32</age> 
     <email>[email protected]</email> 
    </Employee> 
</Employees> 

請注意firstname和員工失蹤1111

我執行以下選擇:

select 
    c1.emplid, 
    fname, 
    lname 
    from(
    select emplid, xmldata from employeeXML 
    LATERAL VIEW explode (xpath(xmldata,'/Employees/Employee/@emplid')) dummyTable as emplid)c1 

    LATERAL VIEW explode (xpath(xmldata,concat('/Employees/Employee[@id="',c1.emplid,'"',']/firstname/text()')))dummyTable2 as fname 
    LATERAL VIEW explode (xpath(xmldata,concat('/Employees/Employee[@id="',c1.emplid,'"',']/lastname/text()'))) dummyTable3 
    as lname; 

預期結果:

1111 NULL  Watson 
2222 Sherlock Holmes 

請注意失蹤的第一個名字是NULL值)

但是我得到以下結果:

2222福爾摩斯

Becasue名字缺少員工1111,我沒有找到第一位員工回到我的查詢中。 有沒有辦法讓員工數據返回,如預期的結果中指出的那樣,名字設置爲NULL和/或缺少空格時? 請幫忙。 感謝,

回答

0

您可以隨時用空字符串拼接的結果,這也許應該是罰款:

concat(/Employees/Employee[@id="..."]/firstname/text(), '') 

這是不是你在蜂巢中使用的串連,但內部的XPath功能,所以你會可能在一行中同時應用XPath和Hive concat。

順便說一句,我想你想用@emplid而不是@id來匹配你的數據?