HiveQL查询变量(动态参数值)的使用

主要用hiveconf来进行变量值的获取,格式如下:
变量赋值:
set pt_day='2016-12-31';
变量引用:
${hiveconf:pt_day}  --> 用在Hql里

参考示例:
set pt_day='2016-12-31';
drop table if exists xx_new_identifier;
create table xx_new_identifier as
select a1.appsource,a1.appkey,a1.identifier,a1.pt_day from (
select appsource,appkey,identifier,${hiveconf:pt_day} pt_day from (
select appsource,appkey,identifier,row_number()over(partition by identifier order by appkey) rk
from bi_all_access_log
where pt_day = ${hiveconf:pt_day}) a
where rk=1) a1
left join
(select identifier
from bi_all_access_log
where pt_day < ${hiveconf:pt_day} ) a2 on a1.identifier=a2.identifier
where a2.identifier is null
;
又例:
set pt_week_his='2016#52';
set pt_week_curr='2017#1';
with tab_new_identifier_byWeek as (
select appsource,appkey,identifier from xx_new_identifier 
where case when weekofyear(pt_day)>=52 and month(pt_day)=1 then concat(year(pt_day)-1,'#',weekofyear(pt_day)) else concat(year(pt_day),'#',weekofyear(pt_day)) end = ${hiveconf:pt_week_his}
-- group by appsource,appkey,identifier
),
tab_access_log_byWeek as (
select identifier from bi_all_access_log
where case when weekofyear(pt_day)>=52 and month(pt_day)=1 then concat(year(pt_day)-1,'#',weekofyear(pt_day)) else concat(year(pt_day),'#',weekofyear(pt_day)) end = ${hiveconf:pt_week_curr}
group by identifier)
select a1.appsource,a1.appkey,count(a1.identifier) 留存设备数  
from tab_new_identifier_byWeek a1
inner join tab_access_log_byWeek a2 on a1.identifier=a2identifier
group by a1.appsource,a1.appkey;


你可能感兴趣的:(#,Hive,Sql)