Surender Raja
Surender Raja

Reputation: 3609

How do we convert a string into Array in hive?

I am using hive 1.1

 hive> select country from releases limit 1;
 OK
 ["us","ca","fr"]

For now country is of type string in hive . How do I convert that into Array[String]?

I tried the below, but it is throwing error

 hive> select country, cast(country as Array[String]) from releases limit 1;
 FAILED: ParseException line 1:48 cannot recognize input near 'Array' '[' 'String' in primitive type specification

Can someone help me to do the typecasting?

Upvotes: 7

Views: 25058

Answers (2)

user10223493
user10223493

Reputation: 1

a way without regexp_extract

SELECT country, split(substr(country, 3, length(country) - 4),'","') AS arr FROM releases LIMIT 1;

Upvotes: 0

David דודו Markovitz
David דודו Markovitz

Reputation: 44991

hive> with releases as (select '["us","ca","fr"]' as country)
    > select  split(regexp_extract(country,'^\\["(.*)\\"]$',1),'","')
    > from    releases
    > ;
OK
_c0
["us","ca","fr"]

Upvotes: 10

Related Questions