Hive中用引數變數在HQL中執行

2021-09-10 22:10:53 字數 1994 閱讀 4703

hive配置屬性儲存於 hiveconf 命名空間中,該命名空間中的屬性是可讀寫的。在查詢語句中插入 '$',就可以通過 hive -hiveconf來替換變數。例如,查詢語句和執行方式如下:

[root]$cat test.sql    #檢視該檔案

select * from $

limit $;

[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($,-15),1,7) and substr($,1,7)

group by pt_month,pt_day

order by pt_month,pt_day;

select

$ calc_date,

$ forecast_month,

sum(case when pt_month=substr($,1,7) then pay_amount else 0 end) cm,

sum(if((substr($,1,7)=$) and (pt_day between concat($,'-01') and $),pay_amount,0)) pyd,

sum(case when pt_month=substr(add_months($,-1),1,7) then pay_amount else 0 end) bm,

sum(case when pt_month=substr(add_months($,-13),1,7) then pay_amount else 0 end) bbm,

sum(case when pt_month=substr(add_months(concat($,'-01'),-13),1,7) then pay_amount else 0 end) bym,

sum(case when pt_month in(substr(add_months($,-3),1,7),substr(add_months($,-2),1,7),substr(add_months($,-1),1,7)) then pay_amount else 0 end) tbm,

sum(case when pt_month in(substr(add_months($,-15),1,7),substr(add_months($,-14),1,7),substr(add_months($,-13),1,7)) then pay_amount else 0 end) tbbm,

sum(case when pt_month in(substr(add_months(concat($,'-01'),-15),1,7),substr(add_months(concat($,'-01'),-14),1,7),substr(add_months(concat($,'-01'),-13),1,7)) then pay_amount else 0 end) tbym

from xxyl0123_pay_basic

;

將hive的hql執行結果儲存到變數中

這裡分別針對shell指令碼和python指令碼舉例 shell指令碼如下 注意 在hive語句左右兩邊使用的是esc鍵下面的點號,不是單引號。usr bin env bashtest1 hive s e select max period value from dw dm.dm guba logi...

Hive中HQL的資料型別

整型 tinyint smallint int bigint 浮點型 float double 布林 boolean 字串 string 時間戳 timestamp array create table ifnot exists test array id int work add array ro...

Hibernate的HQL中in引數設定

平時經常用hibernate,由於習慣表間不建立關聯,所以hql查詢時候經常要用in語句。我最常用的情況有2種 1 in後是個子查詢,如 from a where a.id in select b.aid from b where 這樣是沒問題的,如果a.id 和b.aid是相同的資料型別。2 in...