2

I'm trying to concatenate the element of int array to one string in .

The function concat_ws works only for string arrays, so I tried cast(my_int_array as string) but it's not working.

Any suggestion?

leftjoin
  • 36,950
  • 8
  • 57
  • 116
Kamaney
  • 75
  • 2
  • 7

2 Answers2

4

Try to transform using /bin/cat:

from mytable select transform(my_int_array) using '/bin/cat' as (my_int_array);

Second option is to alter table and replace delimiters:

1) ALTER TABLE mytable  CHANGE COLUMN my_int_array = my_int_array_string string;
2) SELECT REPLACE(my_int_array_string, '\002', ', ') FROM mytable;
leftjoin
  • 36,950
  • 8
  • 57
  • 116
0

It seems that the easiest way is to write a custom UDF to perform this specific task:

public class ConcatIntArray extends UDF {
   public String evaluate(ArrayList<Integer> in, final String delimiter){
       return in.stream().map(u-> String.valueOf(u)).collect(Collectors.joining(delimiter));
   }
} 
Alex Libov
  • 1,481
  • 2
  • 11
  • 20