寻找在蜂巢中设置一个可重用的变量 [英] Looking to set a reusable variable in hive

查看:91
本文介绍了寻找在蜂巢中设置一个可重用的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个如下所示的变量,称为today_date,然后能够在整个查询中将其作为变量重用.以下引发错误.

I'm looking to set a variable like below, called today_date, and then be able to reuse it as a variable throughout the query. The below throws an error.

set today_date = date_format(date_sub(current_date, 1), 'YYYYMMdd')

select account
from table
where data_date = today_date

推荐答案

您仍然需要在set行的末尾添加分号,并用${}包围变量并使用正确的命名空间.

You still need to put a semicolon at the end of the set line, surround your variable with ${} and use the proper namespace.

请注意,定义变量后,这将执行date_format()函数.当您使用变量时,SQL代码将照原样复制.可以将它更多地看作是宏而不是变量.

Note that this will not execute the date_format() function when the variable is defined. When you use the variable the SQL code will just be copied as-is. Think of it as more as a macro than as a variable.

此外,Hive具有多个变量名称空间. 最简单的2个选项是,在定义变量时不那么冗长,而在使用变量时(hiveconf名称空间)则更冗长:

Furthermore, Hive has multiple variable namespaces. The 2 easiest options are either to be less verbose when you define your variable but to be more verbose when you use it (hiveconf namespace):

set today_date = date_format(date_sub(current_date, 1), 'YYYYMMdd');
select account from table where data_date = ${hiveconf:today_date};

或相反(hivevar名称空间)

or the other way round (hivevar namespace)

set hivevar:today_date = date_format(date_sub(current_date, 1), 'YYYYMMdd');
select account from table where data_date = ${today_date};

这篇关于寻找在蜂巢中设置一个可重用的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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