蜂巢-如何从列表类型为

时间:2020-05-13 17:04:02

标签: sql database hadoop hive hql

我有一个名为customer的Hive表,该表有一个名为cust_id的列表类型列,具有以下值: cust_id

[123,234,456,567]

[345,457,67]    

[89,23,34]    

现在,我只想在我的选择查询中读取此特定列cust_id,它可以为所有这些列表值提供以下该列cust_id的单独值:

cust_id

123

234

456

567

345

457

67

89

23

34

基本上,我想从此表中获取cust_id的所有值作为一列,以在其他查​​询的where存在或where in子句中使用这些值。 一个解决方案将不胜感激。

1 个答案:

答案 0 :(得分:2)

AFAIK,这是您从蜂巢手册中寻找的。

Lateral view is used in conjunction with user-defined table generating functions such as explode(). As mentioned in Built-in Table-Generating Functions, a UDTF generates zero or more output rows for each input row.

例如

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