我有2张桌子
客户表
{
"Name": "John Doe",
"Info" : {
"Address": "111 Main Street",
"ID": 2222
}
}
查询表 有2列,客户ID和级别。例如
ID Level
1111 1
1123 4
2234 1
如何编写Hive查询以从我的客户表中识别所有属于1级的客户?
谢谢
答案 0 :(得分:0)
使用ID
从JSON提取get_json_object()
并与查找表联接,添加过滤。
演示:
select s.cust_name, s.Address, s.ID, lkp.level
from
(select
get_json_object(json_col,'$.Info.ID') as ID,
get_json_object(json_col,'$.Name') as cust_name,
get_json_object(json_col,'$.Info.Address') as Address
from
( --replace this subquery with your table
select '{"Name": "John Doe","Info" : {"Address": "111 Main Street","ID": 2222}}' as json_col)s
) s
left join
( --replace this subquery with your table
select stack(4,
1111, 1,
1123, 4,
2234, 1,
2222, 3
) as (ID, Level)
)lkp on s.ID=lkp.ID
where lkp.Level !=1 --filter out level 1
or lkp.level is null --this is to allow records without corresponding level in lkp
;
结果:
OK
cust_name address id level
John Doe 111 Main Street 2222 3
Time taken: 31.469 seconds, Fetched: 1 row(s)