给定两个表:
filtered_locations
包含少量数据(仅几行)
|-------------|
| loc<String> |
|-------------|
| ... |
|-------------|
table_clients
非常大的表(数百万行)
|--------------------------------------------|
| id | name| age |locations <array<String> |
|-----|--------------------------------------|
| | | | [a,b,c..] |
|--------------------------------------------|
我想查询表table_clients
中的filtered_locations
上的值。 主要问题是在table_clients
上查询的字段是array
类型。
所以,我爆炸了列,然后尝试嵌入一个子问题以仅包括filtered_locations
中列出的位置。
我遇到的第一个问题是Hive(至少我正在运行的版本)似乎不接受sub询问Insoide in
或 exists
Statment。
那是我遇到的错误:
编译语句时错误:失败:smanticexception无效 列引用sq_1的定义中的"位置" [ TC.Location(从Feltered_locations fl中选择fl.loc fl)]用作 sq_1
作为替代方案,我尝试使用LEFT JOIN
,但由于explode
调用也不起作用第二个错误
编译语句时出错:失败:smanticexception [错误 10085]:不支持带有横向视图的连接"位置"
with filtered_locations as (
SELECT
'loc1' as loc
union all
'loc2' as loc
)
select
id, name, location
max(age) as max_age
from
table_clients tc
LATERAL VIEW EXPLODE(locations) l as location
-- Ideally this should work!
-- where
-- tc.location in (
-- select fl.loc from filtered_locations fl
-- )
left join filtered_locations fl
on fl.loc = tc.location
group by id, name, location
那是什么解决我问题的最佳解决方案?请注意,table_clients
具有数百万记录!
谢谢
理论上,这应该有效
select *
from table_clients c
lateral view explode(location) e as loc
where e.loc in (select l.loc from filtered_locations l)
;
失败:smanticexception [错误10009]:第6行:8无效的表别名 'e'
...但是由于没有,需要一点工作
select *
from (select *
from table_clients c
lateral view explode(location) e as loc
) c
where c.loc in (select l.loc from filtered_locations l)
;