Hive - 如何从表中读取类型为list的列。

问题描述 投票:0回答:1

我有一个名为customer的Hive表,表中有一列名为cust_id的列表类型,其值如下。cust_id

[123,234,456,567]

[345,457,67]    

[89,23,34]    

现在我想在我的select查询中只读取这个特定的列cust_id,它可以给所有这些列表值作为下面这个列cust_id的单独值。

cust_id

123

234

456

567

345

457

67

89

23

34

基本上,我想获取所有的列的值 cust_id 中的一列,在我的其他查询的where exists或where in子句中使用这些值。如果能有一个解决方案,我们将非常感激。

sql database hadoop hive hql
1个回答
2
投票

AFAIK这是你要从蜂巢手册中寻找的内容。

横向视图与用户定义的表生成函数(如explode())一起使用。正如在 "内置表生成函数 "中提到的,UDTF为每条输入行生成零或多条输出行。

例如

SELECT cust_id
FROM mytable LATERAL VIEW explode(cust_id) mytab AS cust_id;

完整的例子:

drop table customer_tab;
create table customer_tab ( cust_id array<String>);

INSERT INTO table customer_tab select array('123','234','456','567');

INSERT INTO table customer_tab select array('345','457','67');

INSERT INTO table customer_tab select array('89','23','34');

select * from customer_tab;
--      customer_tab.cust_id
--      ["123","234","456","567"]
--      ["345","457","67"]
--      ["89","23","34"]

SELECT mytab.cust_id
FROM customer_tab LATERAL VIEW explode(cust_id) mytab AS cust_id;


mytab.cust_id
123
234
456
567
345
457
67
89
23
34

© www.soinside.com 2019 - 2024. All rights reserved.