Hive: how to pass hivevar in query

2019-07-29 21:28发布

I am trying to set hivevar using the query below. But its not able to detect the date when I use it in a query later.

set hivevar:curr_date = select date_sub(max(partition_date), 1) from mytable;
select * from mytable where partition_date = '${curr_date}' limit 10;
No Results

However, it works when I hardcode the hivevar

set hivevar:curr_date = '2017-03-11';
select * from mytable where partition_date = '${curr_date}' limit 10;

Results:

partition_date  cookie  userid  value
2017-03-11  015ABF9C4C6601016574F15E684C8F14    b1a19464f74d6   Melrose

标签: hive
1条回答
太酷不给撩
2楼-- · 2019-07-29 22:29

Hive variables are nothing but a text replacement mechanism.
The replacement is done before parsing and execution.

hive> set hivevar:v1=se;
hive> set hivevar:v2=l;
hive> set hivevar:v3=ec;
hive> set hivevar:v4=t 1+;
hive> set hivevar:v5=2;
hive> ${hivevar:v1}${hivevar:v2}${hivevar:v3}${hivevar:v4}${hivevar:v5};
OK
3

Passing a query result as an argument to another query can be done from the shell, e.g. -

hive --hivevar x=$(hive -e 'select 1+2') -e 'select ${hivevar:x}*100'
查看更多
登录 后发表回答