HIVE – User Defined Functions

Though, Hive has a list of built in functions, in some scenarios we need user defined functions to be written in Java for some specific use cases.
HIVE User Defined Functions.

We can use two interfaces which can be used to write UDFs for apache Hive.

  • The simple API (apache.hadoop.hive.ql.exec.UDF) can be used as long as our function reads and returns primitive types. Means, basic Hadoop & Hive writable types – Text, LongWritable, IntWritable and DoubleWritable etc.
  • If you plan to write a UDF that deals with embedded data structures, such asList, Mapand Set, then you need to useapache.hadoop.hive.ql.udf.generic.GenericUDF, which is a little more involved.
  • Simple API – apache.hadoop.hive.ql.exec.UDF
  • Complex API – apache.hadoop.hive.ql.udf.generic.GenericUDF

Steps to create Hive-UDF

Step 1:-

Open your Eclipse then create a java Class Name

Step 2:-

Add Jar files to project folder

Step 3 :-

Extend UDF Abstract Class

public class classname extends UDF and you return the value.

Step 4 :-

Implement evaluate() method . This method is called once for every row of data being processed

Step 5:-

Compile and create jar file.

Step 6:-

Add jar file to hive class path.

In hive terminal – add jar <jar file path>

Step 7 :-

Create temporary function in Hive Terminal.

CREATE temporary function Convert as ‘udf.Convert′;

udf represents the package name and Convert represents the program name .

For example:



publicclassConvertextends UDF{

private Text result =new Text();

public Text evaluate(String str){

int number;


float fno=(float) number;

String res=Float.toString(fno);


return result;



Here, We have extended UDF abstract class.

This code converts Int to Float.

Assuming a hive table Demo contains column ID with following data:





Select Convert(ID) from Demo gives following output :





October 27, 2015 9:02 am Published by , ,


Comments are closed here.


Call us to know more