【问题标题】:Hive: Set current year/month/day as a variable and use to define filename [duplicate]Hive:将当前年/月/日设置为变量并用于定义文件名[重复]
【发布时间】:2021-09-10 15:52:25
【问题描述】:

我正在尝试将文件写入 s3,其名称应包含当前年份和月份。为此,我想定义变量来获取年、月和日

set year=year(from_unixtime(unix_timestamp()));
set month=month(from_unixtime(unix_timestamp()));
set day=day(from_unixtime(unix_timestamp()));

CREATE EXTERNAL TABLE abc (col1 int, col2 varchar(100), col3 INT)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE LOCATION "s3://location_${hiveconf:year}${hiveconf:month}${hiveconf:day}/"
TBLPROPERTIES ("skip.header.line.count"="1");

如果我运行脚本 - 'select "${hiveconf:year}";' 我得到 'year(from_unixtime(unix_timestamp()))' 作为输出。

还有其他方法可以在文件名中包含日、月和年吗?

【问题讨论】:

    标签: sql hive


    【解决方案1】:

    据我所知,set 命令永远不会评估函数,这就是它按原样返回字符串的原因。

    更多信息请参见:How to store the output of a query in a variable in HIVE

    一个建议是使用 shell 捕获日期,然后将其作为参数传递给您的 sql 脚本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-06
      • 2016-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多