获取当前日期并将其设置为变量,以便将其用作HIVE中的表名 [英] get the current date and set it to variable in order to use it as table name in HIVE

查看:577
本文介绍了获取当前日期并将其设置为变量,以便将其用作HIVE中的表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取当前日期为YYMMDD,然后将其设置为变量,以便将其用作表名.

I want to get the current date as YYMMDD and then set it to variable in order to use it as table name.

这是我的代码:

set dates= date +%Y-%m-%d;
CREATE EXTERNAL TABLE IF NOT EXISTS dates(
    id STRING,
    region STRING,
    city STRING)

但是该方法不起作用,因为似乎分配是错误的.有什么主意吗?

But this method doesn't work, because it seems the assignments are wrong. Any idea?

推荐答案

Hive不计算变量,而是按原样替换它们,在您的情况下,它将恰好是此字符串'date +%Y-%m-%d'.同样,也不能使用current_date()之类的UDF代替DDL中的表名.

Hive does not calculate variables, it substitutes them as is, in your case it will be exactly this string 'date +%Y-%m-%d'. Also it is not possible to use UDF like current_date() in place of table name in DDL.

解决方案是在shell中计算变量并将其传递给Hive:

The solution is to calculate variable in the shell and pass it to Hive:

在外壳中

dates=$(date +%Y_%m_%d);

hive --hivevar  date="$dates" -f myscript.hql

在脚本中:

use mydb; create table if not exists tab_${hivevar:date} (id int);

或者您可以使用hive -e从命令行执行配置单元脚本,在这种情况下,可以使用shell替换变量:

Or you can execute hive script from command line using hive -e, in this case variable can be substituted using shell:

dates=$(date +%Y_%m_%d);

hive -e "use mydb; create table if not exists tab_${dates} (id int);"

这篇关于获取当前日期并将其设置为变量,以便将其用作HIVE中的表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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