Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we convert a string into Array in hive?

Tags:

hadoop

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?

like image 391
Surender Raja Avatar asked Aug 30 '17 16:08

Surender Raja


People also ask

How do I convert a String to a number in Hive?

Converting a String column to an Integer column or converting a column from one type to another is quite simple in Hive. Simply use the cast function to cast the type from one to another.

How to create an array in Hive?

Hive Array Function This function is used to create array out of integer or string values. Following is the syntax of array function. where, value is of type string or integer. For example, consider following example to create array out of integer values.

Can we convert String [] to String?

So how to convert String array to String in java. We can use Arrays. toString method that invoke the toString() method on individual elements and use StringBuilder to create String. We can also create our own method to convert String array to String if we have some specific format requirements.


1 Answers

hive> with releases as (select '["us","ca","fr"]' as country)
    > select  split(regexp_extract(country,'^\\["(.*)\\"]$',1),'","')
    > from    releases
    > ;
OK
_c0
["us","ca","fr"]
like image 169
David דודו Markovitz Avatar answered Sep 21 '22 08:09

David דודו Markovitz