Hive:如何在查询

时间:2017-03-14 12:12:44

标签: hive

我正在尝试使用下面的查询设置hivevar。但是当我稍后在查询中使用它时,它无法检测到日期。

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

然而,当我对hivevar进行硬编码时它会起作用

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

结果:

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

1 个答案:

答案 0 :(得分:1)

Hive变量只不过是文本替换机制 替换在解析和执行之前完成。

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

将查询结果作为参数传递给另一个查询可以从shell完成,例如 -

hive --hivevar x=$(hive -e 'select 1+2') -e 'select ${hivevar:x}*100'