Hive中用参数变量在HQL中执行

60 篇文章 2 订阅

Hive配置属性存储于 hiveconf 命名空间中,该命名空间中的属性是可读写的。在查询语句中插入 '${hiveconf:变量名}',就可以通过 hive -hiveconf来替换变量。例如,查询语句和执行方式如下:

[root]$cat test.sql    #查看该文件
SELECT * FROM ${hiveconf:tablename}
limit ${hiveconf:var_rows};
[root]$hive -hiveconf tablename='t1' -hiveconf var_rows=10 -f test.sql

需要注意的是:
如果有多个变量,每个变量前都要有参数 -hiveconf
变量赋值等号左右不能有空格(例如var_rows=10不能有空格)

一般查询使用示例:

set forecast_month='2019-01';
set curr_date='2019-01-24';
--set forecast_month=substr(add_months(date_sub(CURRENT_DATE,1),i),1,7);

drop table if exists xxyl0123_pay_basic;
create table xxyl0123_pay_basic as
select pt_month,pt_day,sum(amount) pay_amount
from oss_bi_all_pay
where pt_month between substr(add_months(${hiveconf:curr_date},-15),1,7) and substr(${hiveconf:curr_date},1,7)
group by pt_month,pt_day
order by pt_month,pt_day;

select
${hiveconf:curr_date} calc_date,
${hiveconf:forecast_month} forecast_month,
sum(case when pt_month=substr(${hiveconf:curr_date},1,7) then pay_amount else 0 end) CM,
sum(IF((substr(${hiveconf:curr_date},1,7)=${hiveconf:forecast_month}) and (pt_day between concat(${hiveconf:forecast_month},'-01') and ${hiveconf:curr_date}),pay_amount,0)) PYD,
sum(case when pt_month=substr(add_months(${hiveconf:curr_date},-1),1,7) then pay_amount else 0 end) BM,
sum(case when pt_month=substr(add_months(${hiveconf:curr_date},-13),1,7) then pay_amount else 0 end) BBM,
sum(case when pt_month=substr(add_months(concat(${hiveconf:forecast_month},'-01'),-13),1,7) then pay_amount else 0 end) BYM,
sum(case when pt_month in(substr(add_months(${hiveconf:curr_date},-3),1,7),substr(add_months(${hiveconf:curr_date},-2),1,7),substr(add_months(${hiveconf:curr_date},-1),1,7)) then pay_amount else 0 end) TBM,
sum(case when pt_month in(substr(add_months(${hiveconf:curr_date},-15),1,7),substr(add_months(${hiveconf:curr_date},-14),1,7),substr(add_months(${hiveconf:curr_date},-13),1,7)) then pay_amount else 0 end) TBBM,
sum(case when pt_month in(substr(add_months(concat(${hiveconf:forecast_month},'-01'),-15),1,7),substr(add_months(concat(${hiveconf:forecast_month},'-01'),-14),1,7),substr(add_months(concat(${hiveconf:forecast_month},'-01'),-13),1,7)) then pay_amount else 0 end) TBYM
from xxyl0123_pay_basic
;

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值