Creating custom User-Defined Functions (UDF)
Built-in functions in Hive sometimes do not fit the requirements of a business use case or when data analytics required some custom manipulation of data based on certain conditions. For such cases, the user needs to define custom logic as a UDF and run it over the data.
How to do it…
For writing a custom function in Hive, you will have to extend a Hive class: org.apache.hadoop.hive.ql.exec.UDF
.
Let's understand the concept of creating a custom UDF with the example of creating a function to reverse a string.
The following are the steps to create a custom UDF:
Create a new Java project using any IDE, such as Eclipse. Give this any name; let's say "
HiveUDF
".When writing a custom UDF, there should be two libraries in the
classpath
of the project. To do so:Create a folder "
lib
" under the project.Add the following two
JAR
files to thelib
folder:hadoop-common-2.6.0
hive-exec-1.2.1
Add these
JAR
files to theclasspath
of the project. Right-click on Project...