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

查看:42
本文介绍了获取当前日期并将其设置为变量,以便在 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 从命令行执行 hive 脚本,在这种情况下,可以使用 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天全站免登陆