Hive Array Functions, Usage and Examples

  • Post author:
  • Post last modified:October 28, 2019
  • Post category:BigData
  • Reading time:9 mins read

It is very common to store values in the form of an array in the databases. Later you can use array manipulation functions to manipulate the array types. In this article, we will check how to work with Hive array functions to manipulate array types.

Hive Array Functions, Usage and Examples

Hive Array Functions

Below are some of the commonly used Hive array functions.

Hive Array Function

The very first most used function is array function. This function is used to create array out of integer or string values.

Following is the syntax of array function.

array(value1, value2, value2,...)

where, value is of type string or integer.

For example, consider following example to create array out of integer values.

select array(1,3,5,2,5);

+--------------+--+
|     _c0      |
+--------------+--+
| [1,3,5,2,5]  |
+--------------+--+
1 row selected (0.233 seconds)

Hive Split Function

The Hive split functions split given string into an array of values. This function will split on the given delimiter or a regular expression.

Following is the syntax of split array function.

split(string str, string pat)

where str is a string value to be split and pat is a delimiter or a regular expression.

The Hive split function will return the array type.

For example, consider following example to split string on comma delimiter.

select  split(id,',') 
from (select "abc,bcd,def" as id) as a;

+----------------------+--+
|         _c0          |
+----------------------+--+
| ["abc","bcd","def"]  |
+----------------------+--+
1 row selected (0.254 seconds)

Hive array_contains Array Function

The array_contains function works on the array type and return True if given value is present, otherwise returns False.

Following is the syntax of array_contains Array Function:

array_contains(Array<T>, value)

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.

select  array_contains(split(id,','),'def') 
from (select "abc,bcd,def" as id) as a;

+-------+--+
|  _c0  |
+-------+--+
| true  |
+-------+--+
1 row selected (0.198 seconds)

The array_contains Hive function can be used to search particular value in an array.

Hive map_keys Function

Hive map_keys function works on the map type and return array of key values.

Following is the syntax of map_keys function.

map_keys(Map(K.V))

Where, Map(K.V) is a key value pair map type data .

For example, below example returns only keys from the key value pair map types.

 select map_keys( map('key1','value1','key2','value2','key3','value3')) as keys;

+-------------------------+--+
|          keys           |
+-------------------------+--+
| ["key1","key2","key3"]  |
+-------------------------+--+
1 row selected (0.248 seconds)

Hive map_values Function

Hive map_values function works on the map type and return array of values.

Following is the syntax of map_keys function.

map_values(Map<K.V>)

Where, Map(K.V) is a key value pair map type data.

For example, below example returns only values from the key value pair map types.

select map_values( map('key1','value1','key2','value2','key3','value3')) as keys;

+-------------------------------+--+
|             keys              |
+-------------------------------+--+
| ["value1","value2","value3"]  |
+-------------------------------+--+
1 row selected (0.443 seconds)

Hive sort_array Function

The sort_array function sorts the input array in ascending order according to the natural ordering of the array elements and returns it.

Following is the syntax of sort_array function.

sort_array(Array<T>)

Where, T is a string of type array.

For example, consider following example to sort the array string and return sorted array.

select sort_array(array(1,3,5,2,5));

+--------------+--+
|     _c0      |
+--------------+--+
| [1,2,3,5,5]  |
+--------------+--+
1 row selected (0.302 seconds)

Hive size Array Function

The size array function is used to return the size of an array type variable.

Following is the syntax of size function.

size(Array<T>)

Where, T is a string of type array.

select size(array(1,3,5,2,5));

+------+--+
| _c0  |
+------+--+
| 5    |
+------+--+
1 row selected (0.252 seconds)

Hive sentences Array Function

The sentences array function tokenizes a string of natural language text into words and sentences, where each sentence is broken at the appropriate sentence boundary and returned as an array of words.

Following is the syntax of sentences function.

sentences(string str, string lang, string locale)

Where str is a string. The ‘lang’ and ‘locale’ are optional arguments.

For example, consider below example to tokenize the greeting string.

select sentences('Hello there! How are you?');

+------------------------------------------+--+
|                   _c0                    |
+------------------------------------------+--+
| [["Hello","there"],["How","are","you"]]  |
+------------------------------------------+--+
1 row selected (0.339 seconds)

Related Articles,

Hope this helps 🙂