get the current date and set it to variable in order to use it as table name in HIVE

房东的猫 提交于 2019-12-06 05:58:57

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.

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

In the shell

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

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

In the script:

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

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);"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!