Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert array to string in hive sql?

I want to convert an array to string in hive. I want to collect_set array values to convert to string without [[""]].

select actor, collect_set(date) as grpdate from actor_table group by actor; 

so that [["2016-07-01", "2016-07-02"]] would become 2016-07-01, 2016-07-02

like image 452
Bethlee Avatar asked Aug 02 '16 03:08

Bethlee


People also ask

How to convert array into string in SQL?

In order to convert array to a string, Spark SQL provides a built-in function concat_ws() which takes delimiter of your choice as a first argument and array column (type Column) as the second argument. In order to use concat_ws() function, you need to import it using org. apache.

How to convert a string to array of string in Spark scala?

Spark split() function to convert string to Array column. Spark SQL provides split() function to convert delimiter separated String to array (StringType to ArrayType) column on Dataframe. This can be done by splitting a string column based on a delimiter like space, comma, pipe e.t.c, and converting into ArrayType.

Can array be stored in hive?

Hive array_contains Array Function Where, T is an array and value is the value that you are searching in the given array. For examples, consider following example to search 'def' value in the array. The array_contains Hive function can be used to search particular value in an array.


2 Answers

Use concat_ws(string delimiter, array<string>) function to concatenate array:

select actor, concat_ws(',',collect_set(date)) as grpdate from actor_table group by actor; 

If the date field is not string, then convert it to string:

concat_ws(',',collect_set(cast(date as string))) 

Read also this answer about alternative ways if you already have an array (of int) and do not want to explode it to convert element type to string: How to concatenate the elements of int array to string in Hive

like image 147
leftjoin Avatar answered Sep 21 '22 13:09

leftjoin


Sometimes, you may need a JSON formatted list, so you can simply use:

SELECT CAST(COLLECT_SET(date) AS STRING) AS dates FROM actor_table  

PS: I needed this but found only your question about array to string conversion.

like image 20
Tavy Avatar answered Sep 19 '22 13:09

Tavy