I need to concatenate column values into a single column.
I have column names in a variable as colnames=col1,col2,col3
.
I am writing the below query from a unix shell and calling the hive. But when I do this, I am getting only the column names concatenated not the values of those columns.
select concat('regexp_replace("${colnames}",",","^")) as result from table;
I would like the output as:
ABCD^10^XYZ
(ABCD
,10
,XYZ
are the column values)
You need
concat_ws
function to concatenate values with^
as a delimiter.Example with constants:
Command with column names after shell variable substitution should look like this:
In the shell it will look like this:
If columns are not string, wrap them with cast as string using shell, this will allow concat_ws work with strings and not-string columns.
Example
Output:
Use new variable to pass to hive as in the previous example.