Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Concatenating variables in Hive

avatar
New Contributor

Hello,

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 ",";


***************************************************************
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 ",";

This too is returning an error.


Br
Sandeep

1 REPLY 1

avatar
Super Guru
This will not work.

set hivevar:tab_dt= substr(date_sub(current_date,1),1,10);

Only sets variable hivevar:tab_dt to be string "substr(date_sub(current_date,1),1,10)", not the value as the result of evaluation of the function call.

You will need get the date string outside of Hive and then pass in as the variable.

So below will work:

set hivevar:tab_dt=2017_10_01;
create table test_${hivevar:tab_dt} (a int);