hive查询

yqkkidmi  于 2021-06-01  发布在  Hadoop
关注(0)|答案(1)|浏览(345)

我有两张table,
带有json serde的customer表,其中只包含一列
另一个是查找表,用于定位客户级别(1、2、3、4)
客户表

{ 
  "Name": "John Doe",
  "Info" : {
      "Address": "111 Main Street",
      "ID": 2222
  }
}

查找表有两列,customer id和level。例如

ID     Level
1111    1
1123    4
2234    1

如何编写配置单元查询来标识客户表中级别为1的所有客户?
谢谢

klh5stk1

klh5stk11#

提取 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)

相关问题