繁体   English   中英

使用查询表进行Hive查询

[英]Hive query using lookup table

我有2张桌子

  • 带有JSON Serde的Customer表仅包含一列
  • 另一个是用于查找客户级别(1,2,3,4)的查找表

客户表

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

查询表有两列,客户ID和级别。 例如

ID     Level
1111    1
1123    4
2234    1

如何编写Hive查询以从客户表中识别所有级别为1的客户?

谢谢

使用get_json_object()从JSON提取ID ,并与查找表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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM