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
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With