我们将XML数据作为名为XML的单个字符串列加载到Hadoop中.我们试图检索到数据级别进行规范化或将其分解为单行进行处理(你知道,就像一个表!)尝试过爆炸功能,但没有得到我们想要的.
<Reports>
<Report ID="1">
<Locations>
<Location ID="20001">
<LocationName>Irvine Animal Shelter</LocationName>
</Location>
<Location ID="20002">
<LocationName>Irvine City Hall</LocationName>
</Location>
</Locations>
</Report>
<Report ID="2">
<Locations>
<Location ID="10001">
<LocationName>California Fish Grill</LocationName>
</Location>
<Location ID="10002">
<LocationName>Fukada</LocationName>
</Location>
</Locations>
</Report>
</Reports>
Run Code Online (Sandbox Code Playgroud)
我们正在查询更高级别的Report.Id,然后是孩子的ID和名称(位置/位置).以下基本上给出了所有可能组合的笛卡尔积(在这个例子中,8行而不是我们希望的4行).
SELECT xpath_int(xml, '/Reports/Report/@ID') AS id, location_id, location_name
FROM xmlreports
LATERAL VIEW explode(xpath(xml, '/Reports/Report/Locations/Location/@ID')) myTable1 AS location_id
LATERAL VIEW explode(xpath(xml, '/Reports/Report/Locations/Location/LocationName/text()')) myTable2 AS location_name;
Run Code Online (Sandbox Code Playgroud)
试图分组成一个结构然后爆炸,但这会返回两行和两个数组.
SELECT id, loc.col1, loc.col2
FROM (
SELECT xpath_int(xml, '/Reports/Report/@ID') AS id,
array(struct(xpath(xml, '/Reports/Report/Locations/Location/@ID'), xpath(xml, '/Reports/Report/Locations/Location/LocationName/text()'))) As foo …Run Code Online (Sandbox Code Playgroud)