我的用例是我在配置单元中有一张表,该表的一列为INT,另一列为Array数据类型。我想水平显示它。
最佳答案
爆炸功能将集合数据类型的每个元素显示为单行。
CREATE TABLE Products
(id INT, ProductName STRING, ProductColorOptions ARRAY<STRING>);
select * from products;
1 Watches [“Red”,”Green”]
2 Clothes [“Blue”,”Green”]
3 Books [“Blue”,”Green”,”Red”]
select explode(ProductColorOptions ) from products;
Red
Green
但是我想加入其他列,例如id,但这会导致错误。
在这种情况下,我们需要使用横向 View 。横向 View 为爆炸的列创建一个虚拟表并与基表进行联接。
我们不必担心虚拟表,因为它是在内部由 hive 完成的。
SELECT p.id,p.productname,colors.colorselection FROM default.products P
LATERAL VIEW EXPLODE(p.productcoloroptions) colors as colorselection;
1 Watches Red
1 Watches Green
2 Clothes Blue
关于view - hive 侧 View ,带有带有1列作为阵列的 hive 表的示例示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33993123/