【问题标题】:get the current date and set it to variable in order to use it as table name in HIVE获取当前日期并将其设置为变量,以便将其用作 HIVE 中的表名
【发布时间】:2018-11-13 12:18:24
【问题描述】:

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

这是我的代码:

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

但是这种方法不起作用,因为分配似乎是错误的。有什么想法吗?

【问题讨论】:

    标签: variables hadoop hive hiveql


    【解决方案1】:

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

    解决方案是在shell中计算变量并传递给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 替换变量:

    dates=$(date +%Y_%m_%d);
    
    hive -e "use mydb; create table if not exists tab_${dates} (id int);"
    

    【讨论】:

      猜你喜欢
      • 2020-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 2013-11-23
      相关资源
      最近更新 更多