Hive 变量串联
问题描述:
我在将变量的值与字符串连接时遇到问题.我的脚本包含以下内容
I am facing problems in concatenating the value of a variable with a string . my script contains the below
set hivevar:tab_dt= substr(date_sub(current_date,1),1,10);
CREATE TABLE default.udr_lt_bc_${hivevar:tab_dt}
(
trans_id double
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ",";
在上面,变量 tab_dt 以 yyyymmdd 格式正确分配了昨天的日期.但是当我尝试将此变量与静态字符串连接到表名中时,脚本失败.它不是在做串联.请提供解决方案.
in the above, the variable tab_dt gets assigned correctly with yesterdays date in the format yyyymmdd. but when i try to concatenate this variable in a table name with a static string, the script fails. it is not doing the concatenation . Kindly provide a solution.
注意:我也尝试了以下方法,但也出错
note: i tried the below too, which is erroring out too
set hivevar:tab_dt= substr(date_sub(current_date,1),1,10);
set hivevar:tab_nm1= default.udr_lt_bc_;
set hivevar:tab_name= concat(${hivevar:tab_dt},${hivevar:tab_nm1})
CREATE TABLE ${hivevar:tab_name}
(
trans_id double
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ",";
这也会返回错误.
答
Hive 的另一种方式:
Another way in Hive:
select concat("table_",date_sub(from_unixtime(unix_timestamp(current_date,'yyyy-MM-dd'),'yyyy-MM-dd'),0));
在这里,我们可以在变量中使用上面的内容,并根据需要使用它.
Here, we can use above in a variable and use it as per our needs.