Apache Hive Built-In Functions | Hive Functions
Basically, to perform several operations there are some functions available. Similarly, in Hive also there are some built-in functions available. Such as Hive Collection Functions, Hive Date Functions, Hive Mathematical Functions, Hive Conditional Functions and Hive String Functions.
So, this blog contains the whole concept of Hive Built-In Functions. At first, we will understand what are Hive functions actually mean. Afterward, we will learn the whole concept of Hive Built-in Functions. Also, we will cover the description of each function in detail.
Hive Functions
So, to perform several operations there are some functions built for a specific purpose. Such as Mathematical, arithmetic, logical and relational on the operands of table column names.
Introduction to Hive Built-In Functions
In Hive, there are some built-in functions available. Basically, to use Hive built-in functions in our applications first we need to check the application requirement. However, it is possible to call these functions directly in our application.
In addition, there are several types of Hive Built-in Functions available.
Now, let’s describe Hive Built-in Functions in detail:
a. Collection Functions
Basically, as par its name we use “Collection Functions” for collections.
Here, collections are nothing but defined as a grouping of elements and returning single or array of elements depends on return type mentioned in the function name. Let’s discuss each Collection Functions- Hive Built-in functions in below table:
Return Type | Function Name | Description |
INT | size(Map<K.V>) | It will fetch and give the components number in the map type |
INT | size(Array<T>) | It will fetch and give the elements number in the array type |
Array<K> | Map_keys(Map<K.V>) | It will fetch and gives an array containing the keys of the input map. Here array is in unordered |
Array<V> | Map_values(Map<K.V>) | It will fetch and gives an array containing the values of the input map. Here array is in unordered |
Array<t> | Sort_array(Array<T>) | sorts the input array in ascending order of array and elements and returns it |
b. Hive Date Functions
However, to perform date manipulations and conversion of date types from one type to another type we use Hive date Functions. Let’s discuss each Date Function – Hive Built-in Functions in below table:
Function Name | Return Type | Description |
Unix_Timestamp() | Unix_Timestamp() | We will get current Unix timestamp in seconds |
To_date(string timestamp) | string | It will fetch and give the date part of a timestamp string: |
year(string date) | INT | It will fetch and give the year part of a date or a timestamp string |
quarter(date/timestamp/string) | INT | It will fetch and give the quarter of the year for a date, timestamp, or string in the range 1 to 4 |
month(string date) | INT | It will give the month part of a date or a timestamp string |
hour(string date) | INT | It will fetch and gives the hour of the timestamp |
minute(string date) | INT | It will fetch and gives the minute of the timestamp |
Date_sub(string starting date, int days) | string | It will fetch and gives Subtraction of number of days to starting date |
Current_date | date | It will fetch and gives the current date at the start of query evaluation |
LAST _day(string date) | string | It will fetch and gives the last day of the month which the date belongs to |
trunc(string date, string format) | string | It will fetch and gives date truncated to the unit specified by the format. Supported formats in this : MONTH/MON/MM, YEAR/YYYY/YY. |
c. Mathematical Functions
For Mathematical Operations in Hive, we use “Mathematical Functions”. Though, we have some inbuilt mathematical functions in Hive despite creating UDFs. Let’s discuss each Mathematical Function- Hive Built-in Functions in below table:
Function Name | Return Type | Description |
round(DOUBLE X) | DOUBLE | It will fetch and returns the rounded BIGINT value of X |
round(DOUBLE X, INT d) | DOUBLE | It will fetch and returns X rounded to d decimal places |
bound(DOUBLE X) | DOUBLE | It will fetch and returns the rounded BIGINT value of X using HALF_EVEN rounding mode |
floor(DOUBLE X) | BIGINT | It will fetch and returns the maximum BIGINT value that is equal to or less than X value |
ceil(DOUBLE a), ceiling(DOUBLE a) | BIGINT | It will fetch and returns the minimum BIGINT value that is equal to or greater than X value |
rand(), rand(INT seed) | DOUBLE | It will fetch and returns a random number that is distributed uniformly from 0 to 1 |
d. Conditional Functions
While it comes to conditional values checks in Hive, we use “Conditional Functions”. Let’s discuss each Conditional Function- Hive Built-In Functions in below table:
Function Name | Return Type | Description |
if(Boolean testCondition, T valueTrue, T valueFalseOrNull) | T | It will fetch and gives value True when Test Condition is of true, gives value False Or Null otherwise. |
ISNULL( X) | Boolean | It will fetch and gives true if X is NULL and false otherwise. |
ISNOTNULL(X ) | Boolean | It will fetch and gives true if X is not NULL and false otherwise. |
e. Hive String Functions
For String manipulations and string operations in Hive, we call Hive String Functions. Let’s discuss each String Function- Hive Built-In Functions in below table:
Function Name | Return Type | Description |
reverse(string X) | string | It will give the reversed string of X |
rpad(string str, int length, string pad) | string | It will fetch and gives str, which is right-padded with the pad to a length of length(integer value) |
rtrim(string X) | string | It will fetch and returns the string resulting from trimming spaces from the end (right-hand side) of X For example, rtrim(‘ results ‘) results in ‘ results’ |
space(INT n) | string | It will fetch and gives a string of n spaces. |
split(STRING str, STRING pat) | array | Splits str around pat (pat is a regular expression). |
Str_to_map(text[, delimiter1, delimiter2]) | map<String ,String> | It will split text into key-value pairs using two delimiters. |
Conclusion
As a result, in this blog, we have learned all the possible Hive Built-in Functions. Also, we have described types of Hive Built-In Functions, Hive Date Functions, Hive Mathematical Functions, Hive Conditional Functions and Hive String Functions in detail along with their return type.
Still, if any doubt occurs regarding Hive Built-in Functions, feel free to ask in the comment section.
You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google