Hive:如何在查询中传递hivevar [英] Hive: how to pass hivevar in query

查看:615
本文介绍了Hive:如何在查询中传递hivevar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

然而,当我对hivevar进行硬编码时,它可以正常工作

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变量不过是一种文本替换机制。

在解析和执行之前完成替换。

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'

这篇关于Hive:如何在查询中传递hivevar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆