使用查询表进行Hive查询

时间:2018-11-13 20:00:26

标签: hadoop hive

我有2张桌子

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

客户表

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

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

ID     Level
1111    1
1123    4
2234    1

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

谢谢

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)